Find if a textbox is currently selected
20,290
Solution 1
Since you have found document.activeElement
, you can check its nodeName.
if (document.activeElement.nodeName == 'TEXTAREA' || document.activeElement.nodeName == 'INPUT') {
// ....
}
Something like that.
Solution 2
Ok, just figured it out. Here's what I did:
function checkFocus() {
if ($(document.activeElement).attr("type") == "text" || $(document.activeElement).attr("type") == "textarea") {
//Something's selected
return true;
}
}
Solution 3
Extending the accepted answer by a check for editable HTMLDivElements:
if (document.activeElement.nodeName == 'TEXTAREA'
|| document.activeElement.nodeName == 'INPUT'
|| (document.activeElement.nodeName == 'DIV'
&& document.activeElement.isContentEditable)) {
// …
}
Solution 4
$('#yourTextAreaID:focus');
would not work. :) But
$('#yourTextAreaID').focus(function(){
// do something
});
would excecute the //do something
code when the element receives focus.
Solution 5
$('#target').focus(function() {
alert('Handler for .focus() called.');
});
Also Try:
alert($("*:focus").attr("id"));
Author by
Leticia Meyer
Updated on July 17, 2022Comments
-
Leticia Meyer almost 2 years
How would I find if a textbox (or textarea) currently is in focus? I don't care to know which one it is, I just need to know if one is in focus (has the cursor in it). How would I do this with javascript and jquery?
-
Leticia Meyer over 13 yearsOk, but at any given time (not neccesarily when the user clicks a textbox) how would I check if any textbox has focus?
-
Sikshya Maharjan over 13 yearsGiven that the OP doesn't care "which one it is," I'd suggest amending the selector to:
$('textarea:focus, input:text:focus')...
-
Leticia Meyer over 13 yearsI just found document.activeElement. How would I check if that is a textbox or textarea?
-
JakeParis over 13 years@David Thanks, that's cleaner.
-
lonesomeday over 13 yearsI don't recognise the
:focus
pseudo-selector, and nor does the API or a quick test... -
Sikshya Maharjan over 13 years@lonesomeday, interesting catch: since it works under CSS I'd never thought to question whether or not it worked under jQuery.
-
Leticia Meyer over 13 yearsThanks, but how do I get the jQuery object from document.activeElement?
-
Leticia Meyer over 13 yearsSomeone mark this as the answer, it says I have to wait 2 days. :)
-
Sikshya Maharjan over 13 yearsyou're the only one who can mark this as the answer. Also, take a look at the Stack Overflow Markdown help-page, for guidance on how to format your answers, particularly code samples.
-
lonesomeday over 13 yearsNote that this will work on all modern browsers, but it will not work on many outdated browsers. Have a look at this answer for a technique to overcome this.
-
JakeParis over 13 yearsas @lonesome pointed out above, :hover is not a real jquery selector. I thought it was too, but when I checked the documentation, I couldn't find it mentioned.
-
Lenar Hoyt over 7 yearsThis does not check for editable HTMLDivElements. See my answer below.