Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger the DOMNodeInserted event after injecting HTML with jQuery

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.

like image 711
J. Middelkamp Avatar asked Dec 21 '15 10:12

J. Middelkamp


People also ask

How do I use the DOMNodeInserted event with jQuery?

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.

How to bind elements dynamically added to the DOM using jQuery?

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.

What is HTML DOM Events in JavaScript?

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.

What happens when a new node is added to the Dom?

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.


3 Answers

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

like image 173
J. Middelkamp Avatar answered Nov 09 '22 04:11

J. Middelkamp


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-

like image 20
A. Wolff Avatar answered Nov 09 '22 05:11

A. Wolff


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();
like image 42
Harpreet Singh Avatar answered Nov 09 '22 04:11

Harpreet Singh