How to trigger anchor tag click event dynamically in JavaScript?

Raj
Raj
Member
496 Points
21 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: 362
Total Answered: 2
Total Marked As Answer: 1
Posted On: 18-Sep-2023 00:56

Share:   fb twitter linkedin
Answers
kikme
kikme
Member
210 Points
10 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...
Teacher
4822 Points
23 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