Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda