0

I'm trying to append my menu by referring to an external JavaScript file without manually adding the menu in each of my files. How do I go about this? Here is the menu I intend to add. Btw, I'm using bootstrap. I placed the script src before my ending body tag.

                <header class="container">
                    <div class="row">
                        <a href="..\index.html"><img class="col-sm-2" src="..\logo.png" /></a>
                        <nav class="col-sm-10 navbar navbar-inverse">
                            <div class="container-fluid">
                                <ul class="nav navbar-nav navbar-right text-uppercase">
                                    <li><a href="..\index.html">Home</a></li>
                                    <li><a href="..\about.html">About</a></li>
                                    <li class="active"><a href="..\story_1.html">Revenge Is A Dish Best Served Cold</a></li>
                                    <li><a href="..\gallery.html">Gallery</a></li>
                                </ul>
                            </div>
                        </nav>
                    </div>
                </header>
1
  • "innerHTML doesn't seem to work with links" — It does work with links. Commented May 27, 2016 at 12:05

2 Answers 2

1

Just use innerHTML : https://jsfiddle.net/8zaLv0js/

var ul = document.getElementsByClassName('navbar-nav')[0];

ul.innerHTML = ul.innerHTML + "<li><a href=\"#\" onClick=\"alert('hello world!')\">click me!</a></li>";
Sign up to request clarification or add additional context in comments.

2 Comments

No. OP has a doubt about how to append HTML to his DOM. That's one way to do.
This is exactly what I needed. Thanks a lot.
1

In ES6 you could just

Import './template.html' as template;

document.getElementById('container').append(template);

With the actual standard i guess the easiest way is to use JQuery

$('container').load('./template.html');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.