Add an item when the DOMNodeInserted event is raised

I want to add an element after each like button (chrome extension). Since posts are added to the feed without refreshing the page, I have to add an event listener " DOMNodeInserted". But when I try to include a function in it after(), it does not work.

the code:

    $("#contentArea").addEventListener("DOMNodeInserted", function(event) {
        $(".like_link").after('<span class="dot"> · </span><button class="taheles_link stat_elem as_link" title="תגיד תכל&acute;ס" type="submit" name="taheles" onclick="apply_taheles()" data-ft="{&quot;tn&quot;:&quot;&gt;&quot;,&quot;type&quot;:22}"><span class="taheles_default_message">תכל&acute;ס</span><span class="taheles_saving_message">לא תכלס</span></button>');
        $(".taheles_saving_message").hide(); 
    });

When I change $("#contentArea")to document, it resets the whole page.

+5
source share
2 answers

if you want to add an event listener in jQuery, this is not a method, the corect path should always be:

$(document or window).bind( your_event_name, function(event) { your_callback });

with this in mind you can write:

$(document).bind('DOMNodeInserted', function(event) {

    alert('inserted ' + event.target.nodeName + // new node
          ' in ' + event.relatedNode.nodeName); // parent

});

or do what you need to do every time a node is inserted into the DOM.


, addEventListener, javascript, jQuery.

document.addEventListener("DOMNodeInserted", function () {
    // your code
}, false);

: jQuery 1.7, .on. http://api.jquery.com/on/


, ( ) , , node DOMNodeInserted ( node) node DOMNodeInserted...

, ...

span ...

+7

, jQuery.on(), addEventListner, . //

document.addEventListener('DOMNodeInserted', function(e) {
    $("selector").on('DOMNodeInserted', function(e)   {
       //your code which you want to execute each time DOMNodeInserted
    });
});
0

All Articles