title: How to Fix 'One Closing Button Closes All Modals'
body: <p>I'm trying to create a <strong>vanilla JavaScript</strong> Modal that has the capability of being customized by the User, when instantiating it from the HTML file (or JS file). However, when it comes to dealing with the close() function to close the modal, instead of closing ONE modal at a time, using its close button, the close button of the FIRST modal closes ALL modals of the page. I'm not sure what I'm doing wrong...</p>
  
  <p>I've researched other similar vanilla JavaScript, customizable modal libraries, but most of them use either jQuery, some framework, or include a lot of complications that I am not familiar with (I'm still a beginner). I've researched on GitHub, CodePen, Google, and on here; but I have yet to find a solution that satisfies what I need.</p>
  
  <p>Since the code is quite long, I'd suggest you go directly to my CodePen account, where you can have the full code.</p>
  
  <p><a href="https://codepen.io/jdriviere/pen/zYOyJvv?editors=0011" rel="nofollow noreferrer">https://codepen.io/jdriviere/pen/zYOyJvv?editors=0011</a></p>
  
  <p>But here is my close() function:</p>
  
  <pre><code>Modal.prototype.close = function() {
    let modal = document.getElementById(this.options.id);
    let modalBody = modal.children[0];
  
    // Delete elements from Modal Body
    for (let i = 0; i &lt; modalBody.children.length; i++) {
      modalBody.removeChild(modalBody.children[i]);
    } // End of LOOP
  
    // Delete Modal Body from Modal
    modal.removeChild(modalBody);
  
    // Delete Modal from DOM
    modal.style.display = 'none';
    document.body.removeChild(modal);
  
    return this;
  };
  </code></pre>
  
  <p>I would expect the code to close ONE modal at a time, and preferably the modal that has the proper ID (which should be either assigned by the User or by default have a "NoID" ID). Instead, if I close subsequent modals, it closes them; but if I close the FIRST one, it closes ALL of them. Also, is there a way to init() the Modal functionalities as soon as you create the modal instance (I hate manually initiating them)? If so, please include your solution here too, if not much asked.</p>
  
  <p>Been at it for quite some time now. Your help would be greatly appreciated.</p>
  
  <p>Thank you. :)</p>