user2651906
user2651906

Reputation: 193

How to trigger a anchor tag click event dynamically using javascript?

I'm trying to trigger a already written click event for an anchor tag using JavaScript but it is not working. can any one please help me to do this

I have a anchor tag

<a id="memberid">Data Member</a>

<script>
   $("#memberid").click(function() {
       changebreadcrumb("Data Management");
   });
</script> 


i want to trigger above click event on load

i tried
   $("#memberid").click();

and 

   $("#memberid").trigger("click");


but did not work.

Upvotes: 0

Views: 6614

Answers (3)

Gibbs
Gibbs

Reputation: 22956

DEMO

Use jquery and check whether your dom is ready. It works fine. See the properties in the fiddle on its left side panel

  $("#memberid").click(function() {
      alert('clicked');
   });

  $("#memberid").trigger("click");

EDIT: FINAL DEMO

If you want to triger click on pageload, use window.onload in javascript or jquery load method as i mentioned.

 $(window).load(function()
             {
               $("#memberid").trigger("click");
             });  

Upvotes: 1

Ankit Chugh
Ankit Chugh

Reputation: 308

If your click is not working then in that case....try on() function to trigger event

$(document).ready(function(){

    $("#anchor_id").on( "click", function() {
        //your work
    });

});

Upvotes: 0

chaoskreator
chaoskreator

Reputation: 914

Place this at the end of your HTML before the closing body tag

<script>
    $(document).ready(function(){
        $('#memberid').trigger('click');
    });
</script>

Upvotes: 1

Related Questions