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

$('img').live('mouseover', function() {});

Same, but nicer, listens to mouseover events on img elements that may not exist yet. Then again, this will - if I understand correctly - cause the event to bubble up in the DOM until it reaches the root element, where jquery's live callback handler will check what element the event took place on and invoke that callback.



You're spot on that's the cleanest conceptual way to do it in jQuery. In more recent versions of jQuery, though, $.live is technically deprecated (1.7+). The equivalent new syntax is:

$(document).on('mouseover', 'img', function() {});


That creates a event handler for all images on mouseover, even images that haven't been created yet.

You want to trigger the mouseover event, not bind to it. And you want to trigger it after the image has been inserted. Unfortunately there's no jquery event for dom insertions. But on firefox you can use a MutationObserver:

    MutationObserver(function(event) {
      $(event[0].addedNodes[0]).mouseover();
    }).observe($('body')[0], {childList: true});


Whoops, you're 100% correct.


the 5 previous posts in this thread are so perfectly stepwise, it's making my head explode.


That registers a mouseover event listener. Mine triggers a mouseover event. They are completely different.




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

Search: