Compare document.activeElement
with the element you want to check for focus. If they are the same, the element is focused; otherwise, it isn't.
// dummy element
var dummyEl = document.getElementById('myID');
// check for focus
var isFocused = (document.activeElement === dummyEl);
hasFocus
is part of the document
; there's no such method for DOM elements.
Also, document.getElementById
doesn't use a #
at the beginning of myID
. Change this:
var dummyEl = document.getElementById('#myID');
to this:
var dummyEl = document.getElementById('myID');
If you'd like to use a CSS query instead you can use querySelector
(and querySelectorAll
).