How to make a click shoot another click?

6

I would like to know if there is a click on an element by clicking elsewhere, something like this:

jQuery

$("#button1").click(function(){
    $("#button2").click();
});

I tried this and it did not work. Taking the example IDs as real; there are a lot of events linked to #button2 and I think the most practical way to fire them when I click on #button1 is this way, because I can not tweak these events internally or link them to #button1 .     

asked by anonymous 04.07.2014 / 23:21

1 answer

12
$("#button1").click(function(){
    $("#button2").trigger('click');
});

Source: link

Alternative using .on with delegation:

$("#divQueContemOsBotoes").on( "click", "#button1", function() {
    $("#button2").trigger('click');
});


Demo:

$("#button1").click(function(){
  $("#button2").trigger('click');
});

$("#button2").click(function(){
  $("#box").append('<p>Botao 2 clicado</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><divid="box">
  <button id="button1">Botao 1</button>
  <button id="button2">Botao 2</button>
</div>
    
04.07.2014 / 23:25