0

how to call jquery click function from other jquery function.

i tried by using trigger function it is not working.

Here is my jquery function

$('#index1').click(function(event){
    $('#index2').trigger('click');
});

$('#index2').click(function(event){
   alert("Hello");
});

i want to call #index2 function from #index1 function.

6
  • .trigger() should work. Are you trying to click element or simply execute the click handler? You can try $('#index2').get(0).click() Commented Oct 13, 2016 at 11:43
  • It works fine: jsfiddle.net/tk6y0x81 Commented Oct 13, 2016 at 11:43
  • Your code works, so?! If no, then obviously you aren't binding events Commented Oct 13, 2016 at 11:43
  • 1
    Try replacing line 2 with $('#index2').click(); Commented Oct 13, 2016 at 11:49
  • your code is just perfect, just check if you have added a Jquery library or not. Commented Oct 13, 2016 at 12:00

1 Answer 1

2

Here is the working example:

Be sure you have added a jquery library

$('#index1').click(function(event){
    $('#index2').trigger('click');
});

$('#index2').click(function(event){
   alert("Hello");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<h2 id="index1">
Index 1
</h2>


<h4 id="index2">
Index 2
</h4>

Sign up to request clarification or add additional context in comments.

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.