Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda