0

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 1

2

You can use trigger

$( "#foo" ).on( "click", function() {
    alert( $( this ).text() );
});

$( "#foo" ).trigger( "click" );
Sign up to request clarification or add additional context in comments.

3 Comments

You could also just use $('#foo').click();, which looks cleaner.
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
Could you post the section of the code where you are trying to trigger the click event in the ajax func ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.