How to trigger anchor tag click event dynamically in JavaScript?

Raj
Raj
648 Points
26 Posts

Here i'm trying to do linking in a page as:

<div id="CommentAnchor" class="Comment">
            <a id="anchorCommentAnchor" href="#CommentAnchor"></a>
</div>

Here trying dynamically click event:

$('#anchorCommentAnchor').click();

But its not working.

How to trigger anchor tag click event dynamically in JavaScript?

Views: 736
Total Answered: 2
Total Marked As Answer: 1
Posted On: 18-Sep-2023 00:56

Share:   fb twitter linkedin
Answers
kikme
kikme
248 Points
12 Posts
         

Try something:

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

Refer here: http://api.jquery.com/trigger/

 

Posted On: 18-Sep-2023 02:03
Rahul Maurya
Rahul M...
4916 Points
27 Posts
         

Try to use as:

$('#anchorCommentAnchor')[0].click();
Posted On: 18-Sep-2023 06:26
Thanks. Worked.
 - Raj  18-Sep-2023 06:40
 Log In to Chat