In one of our projects we load slides into our webpage with use of AJAX. After the slides are loaded I want jQuery to execute a plugin on all new injected elements automatically.
This is the code I found else where but it didn't do the trick. Also I've tried to replace .on
function with the .bind
function but then the whole site died and JavaScript crashes with an overflow.
function loaded(selector, callback) {
//trigger after page load.
jQuery(function () {
callback(jQuery(selector));
});
//trigger after page update eg ajax event or jquery insert.
jQuery("body").on('DOMNodeInserted', selector, function (e) {
callback(jQuery(this));
});
}
I got the problem reproduced in a JSFiddle.
Here's where the DOMNodeInserted event comes into play. To properly use this event with jQuery you need the bind () method. Passing the event object to the event handler allows you to access its target property to make a test on the current element.
If you try to bind the elements that are dynamically added to the DOM using the click () method of jQuery, this will not work, because it only binds the click event to elements that exist at the time of the “binding”. To bind the click event to all existing and future elements, use jQuery’s on () method. see the following example.
JavaScript HTML DOM Events. HTML DOM allows JavaScript to react to HTML events: Reacting to Events. A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
The DOMNodeInserted event fires when a new node is added to the DOM. Here's a practical use case with jQuery. What happens to the DOM when a new node is inserted? The new DOM specifications answer this question with the introduction of a new event, namely DOMNodeInserted.
A. Wolff,
Thanks your answer solves my problem. I have edited the loaded function to the following:
function loaded(selector, callback) {
//trigger after page load.
jQuery(function () {
callback(jQuery(selector));
});
var parentSelector = "* > " + selector;
//trigger after page update eg ajax event or jquery insert.
jQuery(document).on('DOMNodeInserted', parentSelector, function (e) {
callback(jQuery(this).find(selector));
});
}
Also I've forked a new working JSFiddle project for anyone who wants a full working example.
https://jsfiddle.net/9t8cahqv/
Thanks,
Jop
Event is fired on container DIV level so your selector isn't matching. You could use instead:
loaded(":has([title])", function(element) {
element.tooltip();
});
/**/
jQuery(document).on('DOMNodeInserted', selector, function(e) {
callback(jQuery(this).find('[title]'));
});
-jsFiddle-
Another way of doing this might be, watch for a DOM
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (!mutation.addedNodes) return
for (var i = 0; i < mutation.addedNodes.length; i++) {
// do things to your newly added nodes here
var node = mutation.addedNodes[i]
if ($(node).hasClass("some class")) {
$(node).remove()
}
//or with id
if ($(node).attr("id") == "someId") {
$(node).remove()
}
//or any other selector
}
})
})
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
})
To Stop Observe, use
observer.disconnect();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With