I have a jQuery plugin that I am using:
$(document).ready(function(){
$('.elements').fancyPlugin();
});
This works great until I start adding new elements:
$.get('ajax.html', function(data){
$('#container').html(data);
});
I could call the plugin function again like this:
$.get('ajax.html', function(data){
$('#container').html(data).find('.elements').fancyPlugin();
});
...except that the AJAX is happening inside another jQuery plugin that shouldn't have to know about the fancyPlugin()
.
How can I apply this plugin to all current and future elements?
I think this will work in all browsers except IE:
document.body.addEventListener("DOMNodeInserted", function(event){
var $elementJustAdded = $(event.target);
if ($elementJustAdded.hasClass('elements')) {
$elementJustAdded.fancyPlugin();
}
}, false);
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