Is there the equivalent of the jQuery livequery plugin for jQuery 1.7+ ?
I'm trying to dynamically bind events, reading the events a DOM element should bind on based on data-* elements.
<a href="#" class="js-test" data-events="click">Test 1</a>
<a href="#" class="js-test" data-events="mouseover">Test 2</a>
.. etc ..
I want to bind all elements with class .js-test
but only on the events listed in their data-events
attribute.
jQuery.on/live/bind/delegate all require the events to be passed in as params.
This is find for DOM elements that exist on the page when document.ready
, however as I update the DOM (AJAX, JS, etc.) I want any new elements with class .js-test
to have its events bound as well.
The livequery plugin (which is old, from jQuery 1.3 times) seems to allow this, as it simple requires a selector and a function to run against anything that matches the selector.
As of jQuery 1.7 the
on
method, supercedes the live method. While it doesn't have an easy method of passing in or matching selectors like you describe, it is possible to accomplish this by passing in the dynamic value ofdata-events
in place of the event type, as long as the data-event value matches that event.However, since the argument passed into the on method's event parameter -- the first parameter -- is taken from each data-events attribute, from each element in the set of matched elements, we must loop through the collection of matched elements so that we access each elements' individual data-events attribute value separately:
Since you want to map all of the events to a single function, this solution meets your specific requirements, and solves your problem.
However, should your requirements change and you find you need to map a collection of function events to match each event type, this should get you started:
UPDATE:
This has been tested and does indeed work for new elements added to the div#container. The problem was in the way the
on
method functions. The delegating nature ofon
only works if the parent element is included in the selector, and only if a selector is passed into the second parameter, which filters the target elements by data-events attribute:HTML:
JavaScript:
Additionally, use the following jQuery to add an item to the container to test it:
Try it out:
Here is a jsfiddle that demonstrates the tested and working functionality.