Using Jquery to Test If an Input Has Focus

Using jQuery to test if an input has focus

jQuery 1.6+

jQuery added a :focus selector so we no longer need to add it ourselves. Just use $("..").is(":focus")

jQuery 1.5 and below

Edit: As times change, we find better methods for testing focus, the new favorite is this gist from Ben Alman:

jQuery.expr[':'].focus = function( elem ) {
return elem === document.activeElement && ( elem.type || elem.href );
};

Quoted from Mathias Bynens here:

Note that the (elem.type || elem.href) test was added to filter out false positives like body. This way, we make sure to filter out all elements except form controls and hyperlinks.

You're defining a new selector. See Plugins/Authoring. Then you can do:

if ($("...").is(":focus")) {
...
}

or:

$("input:focus").doStuff();

Any jQuery

If you just want to figure out which element has focus, you can use

$(document.activeElement)

If you aren't sure if the version will be 1.6 or lower, you can add the :focus selector if it is missing:

(function ( $ ) {
var filters = $.expr[":"];
if ( !filters.focus ) {
filters.focus = function( elem ) {
return elem === document.activeElement && ( elem.type || elem.href );
};
}
})( jQuery );

Javascript/jQuery detect if input is focused

With pure javascript:

this === document.activeElement // where 'this' is a dom object

or with jquery's :focus pseudo selector.

$(this).is(':focus');

Is there a 'has focus' in JavaScript (or jQuery)?

There is no native solution but yes there is a more elegant way you can do it:

jQuery.extend(jQuery.expr[':'], {
focus: "a == document.activeElement"
});

You're defining a new selector. See Plugins/Authoring. Then you can do:

if ($("...").is(":focus")) {
...
}

or:

$("input:focus").doStuff();

How do I check if input box is focused with jquery?

$(function(){
$("#name").focus(function(){
//Code here
});
});

If this doesn't work ^^
My guess is that jQuery isn't initialized in your HTML document correctly or your script tag isn't at the bottom of your body tag.

Example: https://codepen.io/HenrikDK/pen/NWGpPbK

How to get the focused element with jQuery?

// Get the focused element:
var $focused = $(':focus');

// No jQuery:
var focused = document.activeElement;

// Does the element have focus:
var hasFocus = $('foo').is(':focus');

// No jQuery:
elem === elem.ownerDocument.activeElement;

Which one should you use? quoting the jQuery docs:

As with other pseudo-class selectors (those that begin with a ":"), it is recommended to precede :focus with a tag name or some other selector; otherwise, the universal selector ("*") is implied. In other words, the bare $(':focus') is equivalent to $('*:focus'). If you are looking for the currently focused element, $( document.activeElement ) will retrieve it without having to search the whole DOM tree.

The answer is:

document.activeElement

And if you want a jQuery object wrapping the element:

$(document.activeElement)

Test if any input has focus

I've never used jwerty, but I'd suggest:

jwerty.key('m', function (e) {
if (e.target.tagName.toLowerCase() !== 'input') {
toggleMenu();
}
});

This tests the target of the event, and, if it's not an input, calls the toggleMenu() function; if it is an input, it does nothing (though you could explicitly return false if you'd prefer).

To account for textarea, as I really should have done, the above could be extended (to add another clause to the if assessment, or the following, switch-based approach could be taken:

jwerty.key('m', function (e) {
switch (e.target.tagName.toLowerCase()) {
case 'input':
case 'textarea':
break;
default:
toggleMenu();
break;
}
});

If the target-element is either an input or textarea, pressing m does nothing, whereas if not either of those two elements the default state is entered and toggleMenu() is called.

Check to see if any Input elements are in focus

Use jQuery's focus pseudoselector

if ( $('input:focus').length > 0 ) {  return; }

Or for your code example

if ( $('input:focus').length == 0) { ... }


Related Topics



Leave a reply



Submit