I use Jquery to add items to the page, those items include links. To address these new elements, I use $(container).on... etc. But how can I trigger a link that was created after the dom was built. In other words I want to programatically "click" an element created on the fly. what format should I use
1 Answer
You can use trigger
$( "#foo" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#foo" ).trigger( "click" );
3 Comments
Redmega
You could also just use
$('#foo').click();, which looks cleaner.Nev
That's interesting. I'd already tried that without success, but I am trying to trigger from within the success function of an ajax call. If I try it outside of that function, it seems to work
DinoMyte
Could you post the section of the code where you are trying to trigger the click event in the ajax func ?