Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add event listener on elements created dynamically

Is possible to add event listener (Javascript) to all dynamically generated elements? I'm not the owner of the page, so I cannot add a listener in a static way.

For all the elements created when the page loaded I use:

doc.body.addEventListener('click', function(e){ //my code },true); 

I need a method to call this code when new elements appear on the page, but I cannot use jQuery (delegate, on, etc cannot work in my project). How can I do this?

like image 782
jenjis Avatar asked Jan 10 '13 13:01

jenjis


People also ask

How do you add an event listener after an element was created dynamically?

To attach event handlers to the dynamically created button, we need to select the button with a class of btn and add an event listener of click . We're saying that onclick of the button, the p tag with a class of moreInfo should display block .

How do you add an event dynamic to an element?

Attaching the event dynamicallyclassName = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

How do you bind a click event to dynamically created elements?

You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event. Show activity on this post. Show activity on this post. You should aim to bind it to the closest static parent not the whole document.

How do I add an event listener to multiple elements?

Adding event listener to multiple elements To add the event listener to the multiple elements, first we need to access the multiple elements with the same class name or id using document. querySelectorAll() method then we need to loop through each element using the forEach() method and add an event listener to it.


2 Answers

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) {   if (event.target.tagName.toLowerCase() === 'li') {     // do your action on your 'li' or whatever it is you're listening for   } }); 

CAVEATS! The example Fiddle only includes code for the standards-compliant browsers (i.e., IE9+, and pretty much every version of everyone else) If you need to support "old IE's" attachEvent, then you'll want to also provide your own custom wrapper around the proper native functions. (There are lots of good discussions out there about this; I like the solution Nicholas Zakas provides in his book Professional JavaScript for Web Developers.)

like image 186
founddrama Avatar answered Sep 20 '22 13:09

founddrama


Depends on how you add new elements.

If you add using createElement, you can try this:

var btn = document.createElement("button"); btn.addEventListener('click', masterEventHandler, false); document.body.appendChild(btn); 

Then you can use masterEventHandler() to handle all clicks.

like image 36
ATOzTOA Avatar answered Sep 18 '22 13:09

ATOzTOA