Why Firefox says that window.event is undefined? (call function with added event listener)

try getting the event using the parameter passed (named e in this case). i tested this and both window.event and the e is supported in chrome. try checking for both, whichever exists var ex = { exampl: function(e){ console.log(window.event); console.log(e); //check if we have “e” or “window.event” and use them as “evt” var evt = … Read more

Javascript: Uncaught TypeError: Cannot call method ‘addEventListener’ of null

Your code is in the <head> => runs before the elements are rendered, so document.getElementById(‘compute’); returns null, as MDN promise… element = document.getElementById(id); element is a reference to an Element object, or null if an element with the specified ID is not in the document. MDN Solutions: Put the scripts in the bottom of the … Read more

Want to add “addEventListener” on multiple elements with same class [duplicate]

You need to use querySelectorAll which will return a collection.Now use spread operator (three dots) to convert it to array and use forEach .Inside forEach callback add the event listener to it […document.querySelectorAll(‘.breakdown’)].forEach(function(item) { item.addEventListener(‘click’, function() { console.log(item.innerHTML); }); }); <button id=’btn-1′ type=”button” name=”first” class=”breakdown main-text”> Breakdown Start </button> <button id=’btn-2′ type=”button” name=”second” class=”breakdown main-text” … Read more

Adding click event handler to iframe

iframe doesn’t have onclick event but we can implement this by using iframe’s onload event and javascript like this… function iframeclick() { document.getElementById(“theiframe”).contentWindow.document.body.onclick = function() { document.getElementById(“theiframe”).contentWindow.location.reload(); } } <iframe id=”theiframe” src=”https://stackoverflow.com/questions/6452502/youriframe.html” style=”width: 100px; height: 100px;” onload=”iframeclick()”></iframe> I hope it will helpful to you….

Correct usage of addEventListener() / attachEvent()?

The usage of both is similar, though both take on a slightly different syntax for the event parameter: addEventListener (mdn reference): Supported by all major browsers (FF, Chrome, Edge) obj.addEventListener(‘click’, callback, false); function callback(){ /* do stuff */ } Events list for addEventListener. attachEvent (msdn reference): Supported by IE 5-8* obj.attachEvent(‘onclick’, callback); function callback(){ /* … Read more

How to remove all listeners in an element? [duplicate]

I think that the fastest way to do this is to just clone the node, which will remove all event listeners: var old_element = document.getElementById(“btn”); var new_element = old_element.cloneNode(true); old_element.parentNode.replaceChild(new_element, old_element); Just be careful, as this will also clear event listeners on all child elements of the node in question, so if you want to … Read more