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

207
Visualizações
Unable to access elements from a file that is included by javascript

I created a single navbar.html and included it in all the pages of my website.j The css works fine but the javascript is unable to select elements from that navbar.

navbar.html:

<nav class="navbar">
    <h1>
        This is a navbar
    </h1>
</nav>

index.html where it is replaced with a placeholder:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
   <div class="nav-placeholder"></div>
    <script src="app.js"></script>
</body>
</html>

app.js that is replacing the navbar and implementing a test function:

$(function(){
    $(".nav-placeholder").load("nav.html");
  });

document.querySelector('.navbar').addEventListener('click', function(){
    alert('clicked');
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're trying to bind your event listener before the load function had inserted the data into the DOM.

Either use event delegation or move the work of binding the event listener into a callback function you pass as the second argument to the load method.

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