aaronstacy aaronstacy - 5 months ago 8x
Javascript Question

Trigger a jQuery keyup event only when an <input> element is not focused

I can use jQuery to bind a function to a keyup event within the context of an

element such that the event only fires when the
element is focused:

$('#my-input').keyup(function(e) {
// only fires when the focus is on the <input> element

How do I bind a function to a keyup event outside the context of the
I want to be able to trigger a keyup event only when the
element is not focused:

$(':not(#my-input)').keyup(function(e) {
// this code fires regardless of whether the input element is focused

Unfortunately, the function above is run regardless of whether the
box is focused.

 $('body').keyup(function(e) {
  //do stuff