John_911
John_911

Reputation: 1170

Chain a div click to another with jquery?

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?

Upvotes: 0

Views: 182

Answers (8)

Charlie G
Charlie G

Reputation: 824

$('#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.

Upvotes: 0

Colton Allen
Colton Allen

Reputation: 3060

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

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

$('#bar').click(function() {
    // Do something
});

Upvotes: 0

TheGr8_Nik
TheGr8_Nik

Reputation: 3200

Yes you can by doing so:

$("#idDiv1").click(function(){
    //do what you want
    $("#idDiv2").trigger("click");
} 

Upvotes: 0

Mirko Cianfarani
Mirko Cianfarani

Reputation: 2103

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

With

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

Documentation

Upvotes: 0

Barmar
Barmar

Reputation: 780808

$("#div1").click(function() {
    // Do stuff...
    // Then click the other DIV
    $("#div2").click();
}

Upvotes: 2

Starscream1984
Starscream1984

Reputation: 3062

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

$("#div2").click();

Upvotes: 1

mr_greb
mr_greb

Reputation: 208

You can just call click() on div 2:

$('#div1').click(function(){
   //your code
   $('#div2').click();
});

Upvotes: 2

David Stetler
David Stetler

Reputation: 1453

you can use:

$('#div1').click(function(){
  $('#div2').trigger('click');
})

Upvotes: 5

Related Questions