I have included the code from my JavaScript file and the header.html which gets loaded into index.html with the help of <div id="header"></div>
. I have all my "script src" before the closing body tag in index.html. When I try to press the button I receive the following error:
Uncaught TypeError: Cannot read property 'addEventListener' of null
I am aware that this has something to do with the way I am loading my html files. Is there a way I could solve this issue?
I am trying to keep the code redundancy more or less low and copying the whole header into every html file is not an option.
/* -- Loading assets -- */
$(document).ready(function() {
$("#header").load("assets/header.html");
$("#footer").load("assets/footer.html");
});
/* -- Mobile hamburger menu --*/
const hamburger = document.getElementById('hamburger');
const navList = document.getElementById('nav-list');
hamburger.addEventListener('click', () => {
navList.classList.toggle('show');
});
/* Seperate header.html file that gets loaded into index.html */
<nav class="navbar" id="navbar">
<p class="logo"><a href="../index.html"><ŽF/></a></p>
<button class="hamburger" id="hamburger">
<i class="fa fa-bars"></i>
</button>
<ul class="nav-list" id="nav-list">
<li class="nav-item"><a href="journey.html">Link1</a></li>
<li class="nav-item"><a href="design.html">Link2</a></li>
<li class="nav-item"><a href="webdev.html">Link3</a></li>
</ul>
</nav>