Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

But can you detect when DOM elements are created? (Which is different than adding a click event to new DOM element.)

I was looking into this issue myself recently and ultimately (through advice on Stackoverflow) decided to just ignore jQuery and to use DOM mutation events. I'm still curious if there is a way to do this using jQuery, though.

While working on this I briefly considered livequery due to this StackOverflow post (http://stackoverflow.com/questions/4818020/livequery-perform...), but soon realized that this only intercepts DOM changes made via jQuery, and won't work with code in the wild (which is what I need since it is for a Chrome extension.)



You can use the DOMNodeInserted event. Something like this:

document.addEventListener("DOMNodeInserted", function(event) { console.log($(event.target).parent()); });

That will log the element that gets inserted into the DOM.


That's roughly what I'm doing now (i.e. DOM mutation events). It just isn't using jQuery.


Is that a problem? You can use jQuery to wrap the event target like my example did. Or just do $(document).bind('DOMNOdeInserted', function () {}); Is that using jQuery enough? I suppose if you knew you were only using jQuery and wanted to modify the source, you could somehow hook into every function that modifies the DOM and go from there (which is what you just described with livequery).

Not really sure why your requirement is it needs to "use" jQuery though.


Not really; my code is working fine as is. I think the "problems" were 1) I was curious if there was a way to do it via jQuery instead of DOM mutation methods (looks like no) and 2) the documentation for these jQuery methods doesn't specifically mention that this case isn't covered and that the developer should look at DOM events, although that is just perhaps assumed to be default behavior when working with jQuery.

I'm writing to the people who maintain the jQuery documentation now...




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: