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

111
Visualizações
How to get element by tag in javascript

I was trying to write JS code to highlight element when user hovers a cursor over it. To achieve it I am trying to add eventListener on every children on first tag "nav" in current document:

let navigation = document.getElementsByTagName("nav");
elements = navigation[0].children;

for (element in elements) {
  elements[element].addEventListener("mouseenter", function(event) {
    event.target.style.color = "#ffbf00";
    event.target.style.textShadow = "0px 0px 20px #ffbf00";
    setTimeout(function() {
      event.target.style.color = "";
      event.target.style.textShadow = "0 0 5px #a50068";
    }, 500);
  }, false);
}

But when I open it in browser, not only it does not work, but also Chrome says in DevTools that variable "navigation" is undefined. What am I doing wrong?

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

0

This can be more easily accomplished using CSS:

nav>*:hover {
  color: #ffbf00;
  text-shadow: 0px 0px 20px #ffbf00;
}

As for your JS:

  • add type="module" or defer attribute to your <script> tags or your DOM won't be loaded when the script is executed and it won't find your nav element.
  • you should loop over the items using for (const element of elements) (instead of in).
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