9

Is it possible to call an jQuery function on newly matched items automatically?

For example I have the following code:

$(document).ready(function(){
    $('[draggable]').draggable();
});

This adds the 'draggable' to each element which matches [draggable] however when further along the road new elements with the attribute 'draggable' are added those are not getting the 'draggable()' function getting called on them.

Is it possible to monitor the DOM or something and also call this method on each new dom item which matches the selector?

I know there is something like this for 'click' events and such (the jquery delegate method) but as far as I know I can't seem to use that for this case.

1

5 Answers 5

4

Check "Mutation Events" there is an event called DOMNodeInserted maybe it helps you

by the way, check: JQuery - use element with DOMNodeInserted

Sign up to request clarification or add additional context in comments.

3 Comments

The first sub-heading on that MDN page says "Avoid using mutation events". The w3 has deprecated them. Performance isn't great, and not all browsers support them anyway.
It is better to add that behaviour manually for sure, but if it is totally needed I don't see other solution (Mutations Observers would be great, but they are not really supported yet)
It's the best solution for browsers that support it, yes. Otherwise you end up with something clunky like adding an appendDraggable() method and using it instead of .append() (which wouldn't be too bad if there weren't so many other ways to add elements).
3

You can use the arrive.js library that I developed for the exact same purpose (it uses MutationObserver internally). Usage:

document.arrive('[draggable]', function(){
    // 'this' refers to the newly created element
    $(this).draggable();
});

Comments

1

there was ".live()" for jQuery, but i see it's deprecated?! don't get the transformation from ".live()" to the new ".on()"-method currently, but take a look @ yourself and maybe ask in their forum... this should be the right way to do...

http://api.jquery.com/live/

1 Comment

.live() (or the newer .delegate() and .on()) lets you attach an event handler that will run for elements added later, but it doesn't let you automatically apply a method like .draggable() to those elements.
1

.on() is what you need if you are running jQuery 1.7 or later. It will run on elements as they are added to the page, as well as those already in place when it's called. If you're using an earlier version, take a look at the .live() method, which has since been deprecated but has the same functionality with added elements.

1 Comment

.on() (or the older .delegate() or older still .live()) will attach an event handler that will run for elements added later, but it doesn't let you automatically apply a method like .draggable() to those elements.
0

Depending on which version of jQuery you're using, look into the .on() method. If I understand what you're looking for here, it should meet your needs.

The equivalent in previous versions of the framework was .live().

1 Comment

.on() (or .delegate() or .live()) will let you attach an event handler that will run for elements added later, but it doesn't let you automatically apply a method like .draggable() to those elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.