In jQuery is there a way of having something like:
if (button.click() || (keydown == 39)) {
//stuff
}
In jQuery is there a way of having something like:
if (button.click() || (keydown == 39)) {
//stuff
}
You can bind() to multiple events:
$(button).bind("click keydown", function (evt) {
if (evt.type == "keydown" && evt.which == 39)
alert("Key 39 pressed");
else if (evt.type == "click")
alert("Clicked!");
});
Example: http://jsfiddle.net/Bymug/
Note that the spacebar and enter keys may also fire the click event on a button.
function stuff(e) { alert('Something happened'); }
$('input[type=button]').click(function(e) {
stuff(e);
});
$(document).keydown(function(e) {
if (e.keyCode == 39) { stuff(e); }
});
You can use the live event and handle multple events:
$('.someClass').live('keydown mouseclick', function(event) {
if (event.type == 'mouseclick') {
// Do something
} else if (event.type == 'keydown' {
if (event.keyCode == '39')
{
// Do something
}
}
});
No, they are separate events, so you have to hook them up separately. You can use a named function to call from each event:
function x() {
...
}
$('.someclass')
.click(x)
.keydown(function(e){
if (e.keyCode == 39) x(e);
});