Search A2Z 24

Question : How to get the focused element with jQuery?


Using jQuery, how can I get the input element that has focus?

Or in other words, how to determine if an input has the focus?

asked August 30, 2015

1 Answers

Dispalying 1 to 1 of 1 |

In Javascript and jQuery many function or methods are there which we can use and get our respective output,As per above mention question we have three methods which we can use and get our output

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

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

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

:focus jQuery selector

Note : 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.

See comments (0) | Comment
Answered 3 Years ago
Dispalying 1 to 1 of 1 |

Write your Answer write code <pre class="brush: xml">---code---</pre>

Login as a member to access answering block !! click-here