0

Not sure if I'm using the correct lingo,

But I want to click a div and when I do it should cause another to be clicked after.

Ex click div 1 then div 2 gets clicked (but not by user, just by JS)

Is this possible?

There is already a huge function attached to div 2 when clicked, so I need to to link the two if that makes sense. Easier hopefully than sorting through lots of code and trying to add it in.

Any help?

John_911
  • 1,124
  • 2
  • 21
  • 38

8 Answers8

5

you can use:

$('#div1').click(function(){
  $('#div2').trigger('click');
})
David Stetler
  • 1,465
  • 10
  • 14
  • also, see this for advantage of using trigger vs .click() http://stackoverflow.com/questions/13505003/jquery-calling-triggerclick-vs-click – David Stetler Apr 10 '14 at 20:24
2

You can just call click() on div 2:

$('#div1').click(function(){
   //your code
   $('#div2').click();
});
mr_greb
  • 208
  • 2
  • 8
2
$("#div1").click(function() {
    // Do stuff...
    // Then click the other DIV
    $("#div2").click();
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

It is possible, in the click handler for div one call

$("#div2").click();
Starscream1984
  • 3,072
  • 1
  • 19
  • 27
0
$('#div1').click(function() {
    $('#div2').click();
});

Edit:

This solves your problem because it attaches a listener to div1 and executes a function whenever div1 is clicked. It just so happens that you want to emit another event for div1, which, in jQuery shorthand, is written with the .click() function.

Giving .click() a function as a parameter sets the callback for the click event, which can manually be called by calling the function without any parameters.

Charlie G
  • 814
  • 9
  • 22
  • 4
    Consider editing your answer to make it more descriptive, e.g. why does this solve the problem at hand? –  Apr 10 '14 at 20:42
0

Yes this is possible creating the function for the click on the 1.

With

$('#iddiv1').click(function(){
   //your code
   $('#iddiv2').click();
});

Documentation

Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39
0

Yes you can by doing so:

$("#idDiv1").click(function(){
    //do what you want
    $("#idDiv2").trigger("click");
} 
TheGr8_Nik
  • 3,080
  • 4
  • 18
  • 33
0

You need to have an onclick event TRIGGER a click on another div.

$('#foo').click(function() {
    $('#bar').trigger("click");
});

$('#bar').click(function() {
    // Do something
});
Colton Allen
  • 2,940
  • 2
  • 23
  • 33