Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two events at the same time

Tags:

jquery

I want to do some actions when class .smth is clicked

$('.smth').click(function() {

and when key is pressed:

$('.txt').bind('keypress', function(e) {

I want to do the same action, so how can I use them both with OR or something like that?

$('.log').click.or.$('.txt').bind('keypress', function(e) {

?

THank you.

like image 639
good_evening Avatar asked Jul 05 '11 10:07

good_evening


People also ask

Whats it called when two events happen at the same time?

Joint probability is a statistical measure that calculates the likelihood of two events occurring together and at the same point in time.

Can two events occur at the same time?

Is it possible for two events happen at the exact same time? No. Even at any one event itself there can be several (or in though-experimental principle even arbitrarily many) distinct participants (encountering and passing each other, momentarily).

How do you say when you have two meetings at the same time?

If you have two meetings scheduled for the same time, you could use the word concurrent, but you're more likely to use the word simultaneous.

When events are happening at the same time?

Synchronous describes two or more things happening at the same time, as in The fire resulted in the synchronous events of the fire alarm going off and the sprinkler system activating.


2 Answers

If this was the same collection of elements you could use:

$(".myclass").bind("click keypress", function(event) {
    //...
});

But as it's different elements you'll have to follow Felix advice and write a function then attach it as the event handler.

like image 132
Shadow Wizard Hates Omicron Avatar answered Oct 04 '22 00:10

Shadow Wizard Hates Omicron


Use a named function instead of an anonymous one.

function handler() {
    //...
}

$('.txt').keypress(handler);
$('.smth').click(handler);
like image 36
Felix Kling Avatar answered Oct 03 '22 23:10

Felix Kling