Using jQuery, how can I get the input element that has the caret's (cursor's) focus?
Or in other words, how to determine if an input has the caret's focus?
Using jQuery, how can I get the input element that has the caret's (cursor's) focus?
Or in other words, how to determine if an input has the caret's focus?
Try this::
Which one should you use? quoting the jQuery docs:
The answer is:
And if you want a jQuery object wrapping the element:
I've tested two ways in Firefox, Chrome, IE9 and Safari.
(1).
$(document.activeElement)
works as expected in Firefox, Chrome and Safari.(2).
$(':focus')
works as expected in Firefox and Safari.I moved into the mouse to input 'name' and pressed Enter on keyboard, then I tried to get the focused element.
(1).
$(document.activeElement)
returns the input:text:name as expected in Firefox, Chrome and Safari, but it returns input:submit:addPassword in IE9(2).
$(':focus')
returns input:text:name as expected in Firefox and Safari, but nothing in IETry this:
How is it noone has mentioned..
I am using IE8, and have not tested it on any other browser. In my case, I am using it to make sure a field is a minimum of 4 characters and focused before acting. Once you enter the 4th number, it triggers. The field has an id of 'year'. I am using..
Will retrieve it without having to search the whole DOM tree as recommended on the jQuery documentation