Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
document.ActiveElement always returns body Angular

So what was happening is like the title says. On (focusout) when I called a method that checks if an element is an document.activeElement (focused), I was always getting the body as a result. Only if I click on another window/inspector tools I was getting a correct result when doing a console.log which was in my case a button element.

Note that this is in Angular.

Solution is below.

component.html:

<button 
  (focusout)="_onFocusOut()" 
  class="typography-button" (keydown)="_onItemKeyDown($event)" 
  (click)="_selectItem(item)" #menuitem>
  {{ item.label }}
</button>

component.ts

_onFocusOut(): void{
  const isActiveElement = this.menuItemElements?.toArray().some(item => {
  // document.activeElement is always body
    return item.nativeElement == document.activeElement;
  });

  if(!isActiveElement){
    // do something
  }

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The solution was just to wrap the whole method logic in a settimeout method:

_onFocusOut(): void{
// add setTimeout with no value as a second parameter
    setTimeout(() => {
      const isActiveElement = this.menuItemElements?.toArray().some(item => {
        return item.nativeElement == document.activeElement;
      });

      if(!isActiveElement){
        this._toggled = false;
      }
    });
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!