Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click or key press

I'm trying to trigger something to happen either when I press the escape key or when I click on an element. I've tried using..

if( $( '#some_id' ).click() || e.keyCode == 27 )
 {
    alert( 'Click or esc' );
 }

But that doesn't seem to be working, is there something else I can use? If I do each individually like..

$( '#some_id' ).click(...);

or..

if( e.keyCode == 27 )

it works without a problem, but I'd prefer to have it working together to avoid code duplication. Sorry if it's something stupid I missed but would really like to get this sorted.

Thanks :)

like image 916
Johnathan Barrett Avatar asked Jan 24 '26 07:01

Johnathan Barrett


1 Answers

Use .bind() .on() (as of jQuery 1.7) passing in multiple events:

$("#some_id").on("click keyup", function (e) {
    if (e.type == "click" || e.keyCode == 27) {
        alert("click or esc");
    }
});
like image 53
gilly3 Avatar answered Jan 26 '26 20:01

gilly3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!