Reputation: 23989
I have a page which in the footer I include jQuery.js
and anotherJsFunctionThing.js
Also in the footer to fire the latter I bind it to an element:
function doIt(){
$('.newElement').anotherJsFunctionThing({...})
}
How can I fire this if the element .newElement
is added into the DOM after the page is loaded?
Currently when I call doIt()
it fails, because, when jQuery.js
and anotherJsFuntionThing.js
were loaded, .newElement
did not exist
This is what I tried but it failed...
function doIt(){
$('.newElement').on('create', function(){
$('.newElement').anotherJsFuntionThing({...})
});
$('.newElement').trigger('create');
}
Any better ideas?
Upvotes: 0
Views: 103
Reputation: 1546
This jQuery plugin worked for me to watch for initialized DOM events. https://github.com/AdamPietrasiak/jquery.initialize
Your code would look something like this:
$(".newElement").initialize(function(){
$(this).anotherJsFunctionThing({...})
});
Upvotes: 0
Reputation: 7668
Since your current tree won't contain the new element yet, you'll start at the top.
$(document).find(".newElement").anotherThing()
Upvotes: 1