Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!