1

I want to set a simple condition inside the loop so that the code inside is only run if any input is focused or if any element in the body is focused. How do I do each thing?

setInterval(function(){
  document.getElementById('clickMe').style.display= "block";  
}, 1000 );

thanks

1
  • Have a look at some of the other posts about how to determine where the focus is. Commented Aug 25, 2011 at 3:51

1 Answer 1

1

Use the W3C document.activeElement proprety to see which element is focused (also documented at MDN).

Note that this property is introduced in HTML5 and may not be widely implemented, so test for support and expect it not to be supported in a reasonable percentage of clients.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought there was a simple solution with js. Isn't there? I'd rather do something that works in all browsers
What RobG posted is the simple solution. If you want it to work in all browsers (including what, Netscape? IE5?) it gets more complicated.
The SO post Which browsers support document.activeElement? might be of interest.

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.