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

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • Have a look at some of the [other posts](http://stackoverflow.com/questions/483741/how-to-determine-which-html-page-element-has-focus) about how to determine where the focus is. – nnnnnn Aug 25 '11 at 03:51

1 Answers1

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.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • I thought there was a simple solution with js. Isn't there? I'd rather do something that works in all browsers – lisovaccaro Aug 25 '11 at 03:38
  • What RobG posted _is_ the simple solution. If you want it to work in _all_ browsers (including what, Netscape? IE5?) it gets more complicated. – nnnnnn Aug 25 '11 at 03:52
  • The SO post [Which browsers support document.activeElement?](http://stackoverflow.com/questions/5318415/which-browsers-support-document-activeelement) might be of interest. – RobG Aug 25 '11 at 04:47