javascript tutorial - [Solved-5 Solutions] DOM element focus - javascript - java script - javascript array
Problem:
How do I find out which DOM element has the focus?
Solution 1:
document.activeElement is now part of the HTML5 working draft specification, but it might not yet be supported in some non-major/mobile/older browsers. we can fall back to querySelector (if that is supported). It's also worth mentioning that document.activeElement will return document.bodycode> if no element is focused - even if the browser window doesn't have focus.
The following code will work around this issue and fall back to querySelector giving a little better support.
Solution 3:
The approach used by Joel S, but I also love the simplicity of document.activeElement. I used jQuery and combined the two. Older browsers that don't support document.activeElement will use jQuery.data() to store the value of 'hasFocus'. Newer browsers will use document.activeElement. I assume that document.activeElement will have better performance.