Reputation: 10210
activeElement property of the document object sets current element that has the keyboard focus.
But i see strange behavior:
If i put mouse over image/anchor, activeElement shows
<body>
If i right click on anchor, activeElement shows
<a href=....
If i right click on image, activeElement shows
<body>
Can someone please exaplain correct behavior?
I am using Firefox.
console.log(document.activeElement);
Upvotes: 3
Views: 282
Reputation: 106027
Only elements that are "focusable" can have focus. Unlike a link or a textarea, for an image element "focus" is (ironically) largely meaningless and so the spec doesn't list it among the elements that must be focusable and most (all?) browsers follow suit.
When you [right-]click on an element it "blurs" the previously-focused element (if any) and, "without another element being explicitly focused in its stead, the user agent should synchronously run the focusing steps for the body element, if there is one"—in other words, since an image is unfocusable it focuses the body element instead.
If you need focus behavior on an image or another unfocusable element the best solution is usually to wrap it in a link.
Upvotes: 3