javascript - Prevent keyup event from firing when input box selected -


i have event want fire whenever r or space pressed, exception of when in input field.

specifically, i'm worried <input id='nameinput' type='text'> dynamically create when click in div , remove onchange/onblur. that's why tried checking if( !$('#nameinput') ), $('#nameinput') jquery object, boolean value true (and hence !$('#nameinput') == false).

$(window).bind('keyup',   function(e) {      var code = (e.keycode ? e.keycode : e.which);     if(code == 32 || code == 82){       if( !$('#nameinput') ){         roll();       }     }   } ); 

i have solution utilizes global boolean variable getting set in onfocus of input field, i'd solution doesn't require global variable if it's possible.

is there way determine element has focus?

you can use document.activeelement know wich element has focus.

if (!$(document.activeelement).is('input, textarea')) {     roll(); } 

you make use of :focus selector.

if (!$(':focus').is('input, textarea')) {      roll(); } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -