Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding event listeners to current and future elements with a particular class

With JQuery, is it possible to add an event listener to any element that currently, or will in the future, have a particular class?

I'm working on a project that makes heavy use of contentEditable, so the DOM is changing, and elements can have classes added and removed as a result of user input.

I would like to be able to say "elements of class X should do Y when clicked", but if I understand correctly, $(".X").click(Y) will only add the event listener to elements that currently have class X.

Furthermore, if an element is no-longer part of class X, then it will still have the click event listener.

How can I do this?

like image 892
sanity Avatar asked Dec 10 '11 19:12

sanity


People also ask

Can you add event listeners to classes?

To add an event listener to all elements with class: Use the document. querySelectorAll() method to select the elements by class. Use the forEach() method to iterate over the collection of elements.

How do you add an event listener to multiple elements with the same class?

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.

Can event listeners be used with any element?

Creating an Event Listener Using JavaScript. You can listen for events on any element in the DOM. JavaScript has an addEventListener() function that you can call on any element on a web page. The addEventListener() function is a method of the EventTarget interface.


2 Answers

Yep. What you're talking about is called event delegation. Here's an example:

$('#container').on('click', '.innerElement', function(){    /// Do stuff }); 

In your case, #container would be an element that is known to exist on page load which will contain the child elements you care about (either now or in the future). This approach takes advantage of event bubbling in the DOM.

As another poster mentioned, the live method will also work -- but it has been deprecated in jQuery 1.7, and is generally not as performant as using more selective delegation (such as the example above).

like image 102
Kevin Ennis Avatar answered Sep 22 '22 09:09

Kevin Ennis


you'll want to use event delegation. jquery 1.7 has made this more abstract than previous versions, but it looks something like this:

$("#myWrappingElement").on("click", ".myclass", function(event){     alert($(this).text()); }); 

this basically adds a click event listener to the #myWrappingElement element, and jquery will automagically look to see what the original event target was and fire the proper function. this means you can add or remove .myclass elements and still have events fire on them.

like image 30
nathan gonzalez Avatar answered Sep 23 '22 09:09

nathan gonzalez