A few things I've read have led me to the ready()
function, but that seems to run as soon as the document itself is ready, regardless of what element you actually call it on. For example, I'm used to running things when the page is loaded like this:
$(document).ready(function() {
window.alert("foo");
});
What I'm trying to do is run a script when an element with a certain is loaded. That element is added to the page dynamically, and not when the page is initially loaded. I've tried this:
$("#element-id").ready(function() {
window.alert("foo");
});
But that runs on page load, regardless of what id I pass in. Whether or not an element with the passed in id exists on the page or not, or is loaded dynamically, the function always runs on page load, and never again.
How do I tell jquery to run a function when an element is loaded? I'm using jQuery 2.1.4.