First off, I don’t want another plugin to do this… jQuery already has the functionality to do 99% of what I want with the live() method.
Basically, I want to change this:
$(".some-button").live("click", function() { alert('yay!'); });
into this:
$(".some-button").live(function() { alert('yay!'); });
So, what I’m saying is that I want to fire a method right away when an element is added to the page… jQuery can already do this SOOO close because that’s how it adds the “click” even in my example above!
How can I achieve this without adding another plugin, but rather, just by using the same functionality as the “live” and “die” methods do?
There isn’t any cross-browser way to do this, and there’s nothing in jQuery itself that allows it.
You’d have to use a plugin, or just manage invoking code for your new elements manually.
The the
live()[docs] method and thedelegate()[docs] method are only for event handling. The code you give them will only respond to browser events, not to DOM manipulation.The reason
.live()won’t do this is because it does not run any code when adding new elements to the DOM. It isn’t monitoring any DOM changes. Rather it is responding to events that bubble to thedocument, and invoking the handler if it matches the selector you gave it.