Here's a fork of your Plunker: http://plnkr.co/edit/xyPqyl20C4sVL11KqNCS?p=preview
What was changed:
The navigation buttons (forward and back) were given an unique class navigate
<a href="#" class="navigate" id="back">back</a>
<a href="#" class="navigate" id="forw">forward</a>
The href
attributes were changed to represent the next and previous pages (id_1
, id_2
, id_X
, ...)
Because the navigation links change with the page, an onclick
listener was added to the document
.
//Listen to all clicks
document.onclick = function(event) {
var el = event.target;
//If we clicked a navigation link
if (el.className == "navigate" && el.nodeName == "A") {
//Change the page
move(el);
}
};
(Source: Click event on dynamic element WITHOUT jQuery )
Created function move()
to change between pages.
function move(el)
{
if (el.getAttribute("href"))
{
//Get hash without #
var hash = el.getAttribute("href").split("#")[1];
//Change content based on hash
document.getElementById("putIn").innerHTML = document.getElementById(hash).innerHTML;
}
}
A better solution would be to move the navigation links outside the #putIn
div and just keep track of what "page" is currently displayed. This would enable you to add / remove pages without the need to update the code.