I was wondering how to achieve jQuery's .live functionality with "traditional" JavaScript. I want something like $('a').live('mouseover', mouseover_func) to be written as usual JavaScript. But how?
Bind a "mouseover" event handler to the <body>
element. In that handler, check the "target" property of each event it catches and see whether its "tagName" property is "A". If so, call the handler.
The "live" feature leverages event "bubbling", which is the name for the browsers process of checking for handlers from the target element to the root of the DOM, one parent at a time. Since each <a>
in your document can ultimately be traced back up to the <body>
, that root node will get all the "mouseover" events that aren't shunted off by lower-level handlers that cancel bubbling (via the "stopPropagation()" method on the event object, or some weird browser-specific way I guess).
Not all events bubble, however. I'm looking around for a good reference ... ok here, the MDC page seems pretty good though a little old maybe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With