Change loaded content in an HTML file with javascript


Some content, in this case a navbar (navbar.php), is loaded into the index.html by Javascript. Now I want to change a navbar list item from "Home" to "Something else" using Javascript. But it seems that the script can not see the loaded content. The console gives an „document.getElementById(…) is null“ error. Following the code:


<!DOCTYPE html>
<html lang="de">
    <script src=""></script>
      $(function() {
        return false;
    <div class="container">
      <div class="row">
          <div id="navbar">

          let myChange = document.getElementById("change").innerHTML;                   
          myChange = "<a href='somethingelse.html'>Something else</a>";


    <li id="change"><a href="index.html">Home</a></li>
    <li ><a href="logout.php">Logout</a></li>

Any idea what is wrong in the code?
Thanks a lot in advance.


You would need to use the load’s callback function for load to know when the content has been added to the document.

$("#navbar").load("navbar.php", function() {
  const myChange = document.getElementById("change");
  myChange.innerHTML = "<a href='somethingelse.html'>Something else</a>";

Answered By – epascarello

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.