Detect a keypress when a div element is visible

gallery, jquery, keypress, lightbox

Solution

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

or

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

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

Problem

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?

Original source