0

I have this for my navbar:

     <div class="dropdown">
            <a href="#header" class="smoothScroll" class="dropdown">Company</a>
            <div class="dropdown-content">
                <a href="index.html#about">About Us</a>
                <a href="site-pictures.html">Portfolio</a>
                <a href="team.html">Leadership Team</a>
            </div>
        </div>

and what I want to do is put it on all of my pages, so i want to create like a component, where i can then place it in every page without copying line for line. like react.

so i will do this:

navbar = {

<div class="dropdown">
        <a href="#header" class="smoothScroll" class="dropdown">Company</a>
        <div class="dropdown-content">
            <a href="index.html#about">About Us</a>
            <a href="site-pictures.html">Portfolio</a>
            <a href="team.html">Leadership Team</a>
        </div>
    </div>

}

then i want to put the navbar component into each page. is this possible using html?

here is what I tried with help from comment:

<li>
        <div id="side-bar"></div>
        <script>

        document.getElementById("side-bar").innerHTML = <div class="dropdown">         <a href="#header" class="smoothScroll" class="dropdown">Company</a>         <div class="dropdown-content">             <a href="index.html#about">About Us</a>             <a href="site-pictures.html">Portfolio</a>             <a href="team.html">Leadership Team</a>         </div>     </div>
      </script>
        </li>
gianluca
  • 49
  • 6

2 Answers2

0

You can use innerHtml document.getElementById("side-bar").innerHTML = '<div class="dropdown"> </div>'

Or review another link on this site How to load another html file using JS

0

what i did, following the help of the comments is this:

  //navbar
document.getElementById("nav-bar").innerHTML = '<ul class="nav-menu list-unstyled"> <li><a href="index.html" class="smoothScroll">Home</a></li> <li> <div class="dropdown"> <a href="#header" class="smoothScroll" class="dropdown">Company</a> <div class="dropdown-content"> <a href="index.html#about">About Us</a><a href="site-pictures.html">Portfolio</a> <a href="team.html">Leadership Team</a>  </div> </div> </li> <li><a href="index.html#services" class="smoothScroll">Services</a></li> <li><a href="#contact" class="smoothScroll">Contact</a></li> </ul>'

in my main.js file, i have that code above. then inside all of my html files, i have this: <div id="nav-bar"></div>. it works perfect.

gianluca
  • 49
  • 6