I've been over all the documentation for live() (which is now gone from jQuery), delegate(), and on(). After eight hours of hitting myself over the head with this problem, I cannot figure out how to fix it. I want to add an anonymous function to a bunch of links that are created from data brought in via AJAJ. I've tried the following code:
$("#more_items_outer").on(
'click',
'.show_more',
function(a){
var target = a.parentElement;
if (target.is(":visible")){
target.hide("slow");
} else {
target.show();
}
}
);
This code works fine when I run it in the JS console on my development browser after the page has loaded. But this code does nothing for these links if it runs before the actual links are created. As I understand it (and as multiple sources, including the official jQuery docs, describe it), the on() function as I've used it in the above code should make things so that any element of class "show_more" that is created as a descendant of any element with the id "more_items_outer" will execute the anonymous function when it is clicked. This should be true even if the new element's creation post-dates the execution of this code. That's not happening on my dev system here, and I'm all ears as to why this might be so. I'm running jQuery 1.9.1, if that helps.
#more_items_outerexist when the page is first loaded andon()is called?