d-_-b
d-_-b

Reputation: 23161

jquery function if one or other event takes place

What would be the best way to write

IF

$("#div1").click()

      OR

$("#div2").hover()

THEN

{ do some function }

I know how to chain events together that share common element, but how would I do multiple events for multiple elements (and sorry if my terminology is incorrect!).

Thanks for your help!

edit: To clarify, I'm wondering if it is possible to have one function run when either of the two aforementioned events takes place. So if A or B happens then do C.

Upvotes: 0

Views: 148

Answers (3)

Shankar Cabus
Shankar Cabus

Reputation: 9792

For 1 element and 2 or more events:

$("#div").on("click hover", function(){
   console.log("execute");
});

For 2 elements, each with 1 event:

function print(a) {
   console.log(a);
}

$("#div1").on("click", function(){
   print("click");
});

$("#div2").on("hover", function(){
   print("hover");
});

Upvotes: 2

mkoryak
mkoryak

Reputation: 57928

whats wrong with:

var fn = function(){ ... }

$("#div1").click(fn);
$("#div2").hover(fn);

Upvotes: 2

austinbv
austinbv

Reputation: 9491

var x = function(e) { console.log(e); };

$("#div1").click(x)
$("#div2").hover(x)

Upvotes: 2

Related Questions