-1

While this works:

var lnk = document.getElementById("lnk");

lnk.onclick = function() {
    modal.style.display = "block";
}

it no longer works with class and getElementsByClassName

mhodges
  • 10,938
  • 2
  • 28
  • 46
elipticus
  • 11
  • 1
  • `getElementsByClassName` returns an array. You have to access them by index or in a for loop to attach the click handler to each one – mhodges Feb 02 '17 at 20:37

1 Answers1

2

getElementsByClassName returns a nodeList which is array like.

So you would have to bind the event to each node in the list.

var lnks = document.getElementsByClassName("lnk");
                    or
var lnks = document.querySelectorAll(".lnk");

lnks.forEach(function(elem) {
    elem.onclick = function() {
      modal.style.display = "block";
    }
});
Sushanth --
  • 55,259
  • 9
  • 66
  • 105