57

I have this HTML which looks like this:

<input type="submit" name="savebutton" class="first button" />
<input type="submit" name="savebutton" class="second button" />

and JS:

jQuery("input.second").click(function(){
   // trigger second button ?
   return false;
});

So how can I trigger the click event from the 2nd button by clicking on the first one? Note that I don't have any control over the 2nd button, neither on the html or the click event on it...

2
  • You say you have no control over the second button, why? is it in an iframe based on a different domain? Commented Aug 24, 2010 at 19:38
  • no, it's in the Wordpress core, and I don't want to modify code from it Commented Aug 24, 2010 at 19:41

8 Answers 8

118

Add id's to both inputs, id="first" and id="second"

//trigger second button
$("#second").click()
Sign up to request clarification or add additional context in comments.

1 Comment

I have an element on my page which does not have a click event, but if I click on it something happens. How can I just simulate the click without knowing wheter it is a mouseup, click or similar event?
30

Well, you just fire the desired click event:

$(".first").click(function(){
    $(".second").click(); 
    return false;
});

Comments

22
jQuery("input.first").click(function(){
   jQuery("input.second").trigger("click");
   return false;
});

Comments

9

You mean this:

jQuery("input.first").click(function(){
   jQuery("input.second").trigger('click');
   return false;
});

Comments

3

By using JavaScript: document.getElementById("myBtn").click();

Comments

2

If it does not work by using the click() method like suggested in the accepted answer, then you can try this:

//trigger second button
$("#second").mousedown();
$("#second").mouseup();

Comments

1

this works fine, but file name does not display anymore.

$(document).ready(function(){ $("img.attach2").click(function(){ $("input.attach1").click(); return false; }); });

Comments

0

use javascript

document.getElementById("myBtn").click();

1 Comment

Welcome to Stack Overflow! Thank you for your answer. Please provide more details about your solution. Code snippets, high quality descriptions, or any relevant information would be great. Clear and concise answers are more helpful and easier to understand for everyone. Edit your answer with specifics to raise the quality of your answer. For more information: How To: Write good answers. Happy coding!

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.