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:
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:
Was this double :( a response to the fact that jQuery is available and shouldn't have needed to be? Or because you just wrote 6 lines of javascript when you could have written 1?
I read it as the first, and had this immediate reaction:
- I'd rather write the 1 line of jQuery over the 6 in vanilla javascript
- I'd rather read the 1 line of jQuery to understand what is going on instead of the 6 in vanilla javascript
- jQuery didn't actually impact your experience much... as you didn't realize it might even be available.
I'd take the 6 lines of vanilla javascript any day over loading the full jQuery library to get away with writing only a single line. If jQuery isn't use anywhere else on the page, that is.