I have created a JS module where I am adding a click event to all links that match a specific selector. Here is an example of how it's done:
var Lightbox = (function () {
var showLightbox = function () {
// this does stuff
};
var init = function () {
var links = document.querySelectorAll(options.selector);
for(var i = 0; i < links.length; i++) {
links[i].addEventListener('click', function() {
showLightbox();
}, false);
}
};
return {
init: init
};
})();
Lightbox.init();
Initially, the links on the page that match the selector work fine. The closeLightbox()
method also works properly. However, when clicking the links for a second time, nothing happens. There are no console errors or any notifications.
Am I making a mistake in adding the event listener?
EDIT: I have made some updates to the code by removing redundant methods. You can find the full code here: http://pastebin.com/mC8pSAV2