zac1987
zac1987

Reputation: 2777

how to combine 3 conditions by "OR" in jquery?

Is there anyway to combine 3 conditions by "OR" in jquery? Eg, $(t_obj).keyup(function(e) || $('#sendResp').click(function(e) || $('.emo').click(function(e) { ? I have tested to combine them but I get error. How to combine them?

Upvotes: 1

Views: 283

Answers (3)

StuperUser
StuperUser

Reputation: 10850

Where you are attempting to combine conditions, you are, in fact, interrupting 3 jQuery shorthand calls to bind() on selectors.

If you don't interrupt the code to bind, the anonymous functions you write will be called when those events are raised.

To get the intended functionality, create your function and bind it to the events on those selectors individually as others have detailed in answers to this question.

Upvotes: 2

DA.
DA.

Reputation: 40673

Maybe this?

function doThing(){...}

$(t_obj).keyup(doThing());
$('#sendResp, .emo').click(doThing());

Upvotes: 5

Alex Jillard
Alex Jillard

Reputation: 2832

If you want them all to call the same function you can just do:

function myEventHandler(e) {
    //do stuff
}

$(t_obj).keyup(myEventHandler);
$('#sendResp').click(myEventHandler);
$('.emo').click(myEventHandler);

Upvotes: 5

Related Questions