0

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:

Screenshot of page after moving script tag

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
chraebsli
  • 184
  • 12

3 Answers3

0

All you're missing is an element that has an ID of 'first-body'

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>
`;
    <! –– You didn't have an element with id of 'first-body' ––>
    <div id="first-body">
    </div>
rook218
  • 644
  • 7
  • 20
0

It do work I can't see the problem here other than you should import the JS file in the end of your HTML file like the example below. Because if you have an <body> with id first-body It does work look the sample code below.

If you don't understand the code please read: HTML DOM innerHTML Property

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>
`;
<!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>



<body id="first-body">
    <!--here should be the navigation with js-->
    <div class="body">
        <!--content-->
    </div>
</body>

<script type="text/javascript" src="navbar.js"></script>

</html>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Filip Huhta
  • 2,043
  • 7
  • 25
-2

Including the above mentioned problem, your html is formatted incorrectly.

explanation:

Wrong:

  <div class = "dropbtn" id = "dropbtn" >

Right:

  <div class="dropbtn" id="dropbtn">

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>
`;
<!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>



<body id="first-body">
    <!--here should be the navigation with js-->
    <div class="body">
        <!--content-->
    </div>
</body>

<script type="text/javascript" src="navbar.js"></script>

</html>

checkout this codepen

https://codepen.io/minsomai/pen/yLMXeQx

Min Somai
  • 567
  • 6
  • 13