JQuery / JavaScript - trigger button click from another button click event

JavascriptJqueryHtmlInput

Javascript Problem Overview


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...

Javascript Solutions


Solution 1 - Javascript

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

//trigger second button
$("#second").click()

Solution 2 - Javascript

Well, you just fire the desired click event:

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

Solution 3 - Javascript

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

Solution 4 - Javascript

You mean this:

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

Solution 5 - Javascript

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

Solution 6 - Javascript

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

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

Solution 7 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - JavascriptcichyView Answer on Stackoverflow
Solution 2 - JavascriptrobertbasicView Answer on Stackoverflow
Solution 3 - JavascriptpulsedemonView Answer on Stackoverflow
Solution 4 - JavascriptSarfrazView Answer on Stackoverflow
Solution 5 - JavascriptSvibhorView Answer on Stackoverflow
Solution 6 - JavascriptGalusView Answer on Stackoverflow
Solution 7 - JavascriptBlackView Answer on Stackoverflow