Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect a keypress when a div element is visible

I am making a light box gallery widget, now i need to detect the keypress and call to a function but only when the #lightbox element is visible, and if it is not, it should ignore the key press, using jquery, i tried the following:

        $("#lightbox").keyup(function(e) {
alert(e.keyCode);
  if (e.keyCode == 27) { alert("a") }   // esc
});

any sugestion guys?

like image 325
Nicholas Wild Avatar asked Dec 09 '25 03:12

Nicholas Wild


1 Answers

if ($('#lightbox:visible').length && e.keyCode==27) {

or

if ($('#lightbox').is(':visible') && e.keyCode==27) {

http://api.jquery.com/visible-selector/

like image 81
Blazemonger Avatar answered Dec 11 '25 20:12

Blazemonger



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!