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

289
Visualizações
Why does composedPath throw an exception when an attribute does not exist, but works when it does exist?

I have an event listener for click events on the document and I am using composedPath to determine if anything in the path includes a certain attribute.

If something in the path does include an attribute I listen for then all is well and everything works.

However, if something in the path does not include an attribute I listen for then it throws an exception:

"Uncaught TypeError: el.hasAttribute is not a function"

Here is a fiddle showing this:

document.addEventListener('click', (event) => {
  const includesAttribute = event.composedPath().some((el) => el.hasAttribute('my-attribute'));
  console.log(`has attribute: ${includesAttribute}`);
});
<button my-attribute>click me, I log true and work fine</button>

<br/>

<button other-attribute>click me, I throw an exception because I don't have the attribute</button>

Why do I get an exception only when the attribute does not exist? Shouldn't the hasAttribute method always be available?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because document does not have the method getAttribute. Your call to some is getting all the way to document when it doesn't find anything; see the logging of nodeName here:

document.addEventListener('click', (event) => {
  const includesAttribute = event.composedPath().some((el) => {
    console.log(el.nodeName);
    return el.hasAttribute('my-attribute');
  });
  console.log(`has attribute: ${includesAttribute}`);
});
<button my-attribute>click me, I log true and work fine</button>

<br/>

<button other-attribute>click me, I throw an exception but now you can see why</button>

Just add a guard:

document.addEventListener('click', (event) => {
    const includesAttribute = event.composedPath().some((el) => el.hasAttribute && el.hasAttribute('my-attribute'));
    console.log(`has attribute: ${includesAttribute}`);
});

document.addEventListener('click', (event) => {
  const includesAttribute = event.composedPath().some((el) => el.hasAttribute && el.hasAttribute('my-attribute'));
  console.log(`has attribute: ${includesAttribute}`);
});
<button my-attribute>click me, I log true and work fine</button>

<br/>

<button other-attribute>click me, I do not throw an exception anymore</button>

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