0

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.
1
  • wrap your jquery code inside $(document).ready(function(){}) block Commented Nov 19, 2014 at 5:16

3 Answers 3

1

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

<script>
    $(document).ready(function(){
        $('#memberid').trigger('click');
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

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");
             });  

2 Comments

Hi, what problem r u facing? Have u wrapped your code inside domready nd included jquery?
i have a click event in side which i have written what has to happen. now on load of the page i want to trigger that click event.
0

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
    });

});

Comments

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.