I have a page that appends and removes inputs from DOM using jquery. Some of them share a class (“numeric”). Once each input is appended, my code should call .numeric() to every new input with “numeric” class. I defined this block at the beginning of the code,
$('input.numeric')
.live('load', function() {
$(this).numeric();
});
but load seems not to be related to appended nodes after DOM is loaded. Ready event isn’t working either.
How could I do that? Thanks mates
Edit: I managed to solve this following @BGerrissen methods in a more general way. I already defined a wrapper for appending (which I called draw), and then each time I append anything, I trigger $(document).trigger('appended'), so it is easy to track anywhere. Then I use
$(document)
.bind('appended', function() {
$('input.numeric').numeric();
});
To do what I need. Cheers
When appending inputs, fire a custom event as well.
Then you can hook events to it for your purpose:
NOTE: As of jQuery 1.7, .live() is deprecated. Use .on()
Or use a helper method for firing the event on append.
NOT ADVISED **
You can also override jQuery’s append() method to always fire the “appended” event.
But this will apply to ALL append() calls, wether you want it or not and might possibly break something as I’ve not investigated stability of this override.