I want to insert this HTML block into an other HTML file using JS, because if I need to edit the navigation, I can just edit it in the JS file:
<div class="navbar">
<div class="dropdown">
<div class="dropbtn" id="dropbtn">
<a id="menu"><img src="files/i/menu.svg" width="30"></img><span id="menu-text">Menü</span></a></div>
<div class="dropdown-content">
<a id="home" href="index.html"><img src="files/i/home.svg" width="30"></img><span>Startseite</span>
</a>
<a id="me" href="about.html"><img src="files/i/me.svg" width="30"></img><span>Über mich</span></a>
<a id="dienst" href="dienstleistungen.html"><img src="files/i/dienst.svg"
width="30"></img><span>Dienstleistungen</span> </a>
<a id="form" href="form.html"><img src="files/i/form.svg" width="30"></img><span>Anfrage</span></a>
</div>
</div>
</div>
What is the easiest way to do this? I already searched but I only saw things to do it all alone. Does it exist an easy way to do this?
I already tried this but I get an error (Uncaught TypeError: Cannot set property 'innerHTML' of null: navbar.js:1
):
document.getElementById('first-body').innerHTML = `
<div class = "navbar" >
<div class = "dropdown" >
<div class = "dropbtn" id = "dropbtn" >
<a id = "menu" >
< img src = "files/i/menu.svg" width = "30" > < /img>
<span id="menu-text">Menü</span >
< /a>
</div >
<div class = "dropdown-content" >
<a id = "home" href = "index.html" >
<img src = "files/i/home.svg" width = "30" > < /img>
<span>Startseite</span >
</a>
<a id = "me" href = "about.html">
<img src = "files/i/me.svg" width = "30" > < /img>
<span>Übermich < /span>
</a >
<a id = "dienst" href = "dienstleistungen.html" >
< img src = "files/i/dienst.svg" width = "30" > < /img>
<span>Dienstleistungen</span >
< /a>
<a id = "form"href = "form.html" >
< img src = "files/i/form.svg" width = "30" > < /img>
<span>Anfrage</span >
< /a>
</div>
</div>
</div>
`;
In my HTML file to insert I use this: <script type="text/javascript" src="navbar.js"></script>
My HTML file:
<!DOCTYPE html>
<html lang='de'>
<head>
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="files/i/favicon.svg" />
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
</head>
<script type="text/javascript" src="navbar.js"></script>
<body id="first-body">
<!--here should be the navigation with js-->
<div class="body">
<!--content-->
</div>
</body>
</html>
Is it possible, that I cant use the <body>
tag with an ID?
If I put the <script>
before or after the </body>
, it looks like this: