Reputation: 1170
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
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
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
Reputation: 3200
Yes you can by doing so:
$("#idDiv1").click(function(){
//do what you want
$("#idDiv2").trigger("click");
}
Upvotes: 0
Reputation: 2103
Yes this is possible creating the function for the click on the 1.
With
$('#iddiv1').click(function(){
//your code
$('#iddiv2').click();
});
Upvotes: 0
Reputation: 780808
$("#div1").click(function() {
// Do stuff...
// Then click the other DIV
$("#div2").click();
}
Upvotes: 2
Reputation: 3062
It is possible, in the click handler for div one call
$("#div2").click();
Upvotes: 1
Reputation: 208
You can just call click()
on div 2:
$('#div1').click(function(){
//your code
$('#div2').click();
});
Upvotes: 2
Reputation: 1453
you can use:
$('#div1').click(function(){
$('#div2').trigger('click');
})
Upvotes: 5