Angularjs $document[0].activeElement instead of $document.activeElement

Spread the love

Question Description

I am writing a placeholder directive using angularjs.

On the click handler i want to check if the element and document.activeElement are the same.

I tried to use $docuemnt.activeElement for that but it was always undefined. But when I used $document[0].activeElement I am getting the currently active element.

Is $document[0].activeElement is the right way to access the currently active element? Or am doing something wrong?

Practice As Follows

No, $document is a wrapped version of document, it is wrapped using jQlite which is a tiny version of jQuery, so $document doesn’t have any method called activeElement because document is inside $document, So you’ll have to use




You could also create a global variable that is a wrapped version of activeElement like so.

var $activeElement = angular.element(document.activeElement);
$activeElement.attr('focused', 'yes'); // Example usage

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.