currently I am trying to implement the change in the class of the element using javascript, and currently the problem is that when i press on the home page, the last page does not get disselected. my javascript code is
var pageLayout = document.getElementById("page-layout");
var contactPage = document.getElementById("contact-page");
var aboutPage = document.getElementById("about-page");
var home = document.getElementById("Home");
home.onclick= function () {
for (var i = 0, j = contactPage.length; i < j; i++){
if(homepages[i].classList.contains("current-page")){
homepages[i].classList.remove("current-page");
}
}
home.classList.add("current-page");
}
my DOM elements
<ul>
<li><a href="#" class="current-page" id="Home">Home</a></li>
<li><a href="#" id="about-page">About</a></li>
<li><a href="#" id="contact-page">Contact</a></li>
</ul>
what could be the issue? how to improve?
edit: i did use the code that day
var pageLayout = document.getElementById("page-layout");
var contactPage = document.getElementById("contact-page");
var aboutPage = document.getElementById("about-page");
var home = document.getElementById("Home");
homepages = [contactPage ,aboutPage, home];
home.onclick= function () {
for (var i = 0, j = homepages.length - 1; i < j; i++) {
if(homepages[i].classList.contains("current-page")){
homepages[i].classList.remove("current-page");
}
}
home.classList.add("current-page");
}
aboutPage.onclick = function () {
for (var i = 0, j = homepages.length - 1; i < j; i++) {
if (homepages[i].classList.contains("current-page")) {
homepages[i].classList.remove("current-page");
}
}
aboutPage.classList.add("current-page");
}
contactPage.onclick = function () {
for (var i = 0, j = homepages.length - 1; i < j; i++) {
if (homepages[i].classList.contains("current-page")) {
homepages[i].classList.remove("current-page");
}
}
contactPage.classList.add("current-page");
}

homepagesbeing defined.