How to check whether dynamically attached event listener exists or not?

I did something like that: const element = document.getElementById(‘div’); if (element.getAttribute(‘listener’) !== ‘true’) { element.addEventListener(‘click’, function (e) { const elementClicked = e.target; elementClicked.setAttribute(‘listener’, ‘true’); console.log(‘event has been attached’); }); } Creating a special attribute for an element when the listener is attached and then checking if it exists.

Javascript – How to detect if document has loaded (IE 7/Firefox 3)

There’s no need for all the code mentioned by galambalazs. The cross-browser way to do it in pure JavaScript is simply to test document.readyState: if (document.readyState === “complete”) { init(); } This is also how jQuery does it. Depending on where the JavaScript is loaded, this can be done inside an interval: var readyStateCheckInterval = … Read more

add event listener on elements created dynamically

It sounds like you need to pursue a delegation strategy without falling back to a library. I’ve posted some sample code in a Fiddle here: http://jsfiddle.net/founddrama/ggMUn/ The gist of it is to use the target on the event object to look for the elements you’re interested in, and respond accordingly. Something like: document.querySelector(‘body’).addEventListener(‘click’, function(event) { … Read more

MSIE and addEventListener Problem in Javascript?

In IE you have to use attachEvent rather than the standard addEventListener. A common practice is to check if the addEventListener method is available and use it, otherwise use attachEvent: if (el.addEventListener){ el.addEventListener(‘click’, modifyText, false); } else if (el.attachEvent){ el.attachEvent(‘onclick’, modifyText); } You can make a function to do it: function bindEvent(el, eventName, eventHandler) { … Read more

addEventListener not working in IE8

Try: if (_checkbox.addEventListener) { _checkbox.addEventListener(“click”, setCheckedValues, false); } else { _checkbox.attachEvent(“onclick”, setCheckedValues); } Update:: For Internet Explorer versions prior to IE9, attachEvent method should be used to register the specified listener to the EventTarget it is called on, for others addEventListener should be used.

addEventListener in Internet Explorer

addEventListener is the proper DOM method to use for attaching event handlers. Internet Explorer (up to version 8) used an alternate attachEvent method. Internet Explorer 9 supports the proper addEventListener method. The following should be an attempt to write a cross-browser addEvent function. function addEvent(evnt, elem, func) { if (elem.addEventListener) // W3C DOM elem.addEventListener(evnt,func,false); else … Read more

addEventListener using for loop and passing values [duplicate]

Closures! 😀 This fixed code works as you intended: // Function to run on click: function makeItHappen(elem, elem2) { var el = document.getElementById(elem); el.style.backgroundColor = “red”; var el2 = document.getElementById(elem2); el2.style.backgroundColor = “blue”; } // Autoloading function to add the listeners: var elem = document.getElementsByClassName(“triggerClass”); for (var i = 0; i < elem.length; i += … Read more