title: Multiple buttons and modals
body: <p>I'm new to JS and although I have bought some books to start learning properly I wondered if anyone could advise with this? I have store that lists many products. On the store page I have added a button to the bottom of each item and want an action when you click a button. I've applied this code, it works but only on the first element anyway - because it based on id's:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;button id="myBtn" class="openmodal"&gt;Open Modal&lt;/button&gt;
  
  &lt;!-- The Modal --&gt;
  &lt;div id="myModal" class="modal"&gt;
  
    &lt;!-- Modal content --&gt;
    &lt;div class="modal-content"&gt;
      &lt;span class="close"&gt;&amp;times;&lt;/span&gt;
      &lt;p&gt;Some text in the Modal..&lt;/p&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>// Get the modal
   window.onload = function(){
  var modal = document.getElementById('myModal');
  
  // Get the button that opens the modal
  var btn = document.getElementById("myBtn");
  
  // Get the &lt;span&gt; element that closes the modal
  var span = document.getElementsByClassName("close")[0];
  
  // When the user clicks on the button, open the modal
  btn.onclick = function() {
      modal.style.display = "block";
  }
  
  // When the user clicks on &lt;span&gt; (x), close the modal
  span.onclick = function() {
      modal.style.display = "none";
  }
  
  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
      if (event.target == modal) {
          modal.style.display = "none";
      }
  }};
  </code></pre>
  
  <p>I've tried changing the JS to accommodate class names like this, but it didn't work:</p>
  
  <pre><code>// Get the modal
   window.onload = function(){
  var modal = document.getElementsByClassName('modal');
  
  // Get the button that opens the modal
  var btn = document.getElementsByClassName("openmodal");
  
  // Get the &lt;span&gt; element that closes the modal
  var span = document.getElementsByClassName("close")[0];
  
  // When the user clicks on the button, open the modal
  btn.onclick = function() {
      modal.style.display = "block";
  }
  
  // When the user clicks on &lt;span&gt; (x), close the modal
  span.onclick = function() {
      modal.style.display = "none";
  }
  
  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
      if (event.target == modal) {
          modal.style.display = "none";
      }
  }};
  </code></pre>
  
  <p>Please can anyone help?</p>
  
  <p>Thanks in advance
  Chris</p>