Reputation: 21759
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.
Upvotes: 6
Views: 15801
Reputation: 2485
you can handle multiple things in one event have a look on this
$(".smth, .txt, .log").bind("click keypress", function(event) {
console.log("key pressed");
});
Upvotes: 0
Reputation: 66397
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.
Upvotes: 15
Reputation: 817208
Use a named function instead of an anonymous one.
function handler() {
//...
}
$('.txt').keypress(handler);
$('.smth').click(handler);
Upvotes: 9