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"));

http://jsfiddle.net/4KVvV/

Share:
20,290
Leticia Meyer
Author by

Leticia Meyer

Updated on July 17, 2022

Comments

  • Leticia Meyer
    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
    Leticia Meyer over 13 years
    Ok, but at any given time (not neccesarily when the user clicks a textbox) how would I check if any textbox has focus?
  • Sikshya Maharjan
    Sikshya Maharjan over 13 years
    Given that the OP doesn't care "which one it is," I'd suggest amending the selector to: $('textarea:focus, input:text:focus')...
  • Leticia Meyer
    Leticia Meyer over 13 years
    I just found document.activeElement. How would I check if that is a textbox or textarea?
  • JakeParis
    JakeParis over 13 years
    @David Thanks, that's cleaner.
  • lonesomeday
    lonesomeday over 13 years
    I don't recognise the :focus pseudo-selector, and nor does the API or a quick test...
  • Sikshya Maharjan
    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
    Leticia Meyer over 13 years
    Thanks, but how do I get the jQuery object from document.activeElement?
  • Leticia Meyer
    Leticia Meyer over 13 years
    Someone mark this as the answer, it says I have to wait 2 days. :)
  • Sikshya Maharjan
    Sikshya Maharjan over 13 years
    you'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
    lonesomeday over 13 years
    Note 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
    JakeParis over 13 years
    as @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
    Lenar Hoyt over 7 years
    This does not check for editable HTMLDivElements. See my answer below.