I'd like to find the currently focused element in the whole document.
I tried using the :focus
pseudoclass introduced with jQuery 1.6:
$(document).find(":focus")
But $(document).find(":focus").length
always returns 0
You should be able to use the activeElement
property of the document
:
var focus = $(document.activeElement);
If you look at the jQuery documentation for :focus
it makes this perfectly clear:
If you are looking for the currently focused element, $( document.activeElement ) will retrieve it without having to search the whole DOM tree.
It's worth noting that if no element currently has focus, activeElement
will refer to the body
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With