2

How do you tell if an input or textarea has focus in native javascript?

This is not a duplicate as it asks for a native Javascript solution!

1
  • You said you need a native JS solution, so the jQuery tag should have not been added, I've just removed that tag. Commented Jun 28, 2014 at 6:04

2 Answers 2

3

To determine if any input/textarea on the page has focus:

if([...document.querySelectorAll("input, textarea")].some(el => el === document.activeElement)) {
  // An input or textarea currently has focus
}

To determine if a particular input/textarea on the page has focus:

if(document.activeElement === textareaEl) {
  // textareaEl currently has focus
}
Sign up to request clarification or add additional context in comments.

3 Comments

if you know the answer then why ask ?
Good question @Riturajratan! I was trying to find a question that answered this although I was not able to. I then came up with a solution of my own and decided to turn it into a question for other people could benefit and enjoy it.
so i think first you search then ask question any way cheers ;)
0

use :focus

$("input, textarea").is(":focus")

see here it will helpful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.