Reputation: 1129
I want to have a standard menu on on the pages on my website. I'm using an include file as shown below:
<div class="header box">
<ul class="nav box">
<li class="current" id="home"><a href="index.php" onclick="current('home')">Home</a></li>
<li class="" id="onlineSchedule"><a href="onlineSchedule.php" onclick="current(this)">Online Schedule</a></li>
<li class="" id="partners"><a href="partners.php" onclick="current(this)">Partners/Centers</a></li>
<li class="" id="services"><a href="services.php" onclick="current(this)">Services</a></li>
<li class="" id="products"><a href="products.php" onclick="current(this)">Products</a></li>
<li class="" id="newsHighlights"><a href="newsHighlights.php" onclick="current(this)">News/Highlights</a></li>
<li class="" id="signup"><a href="signup.php" onclick="current(this)">Sign Up!</a></li>
<li class="" id="feedback"><a href="feedback.php" onclick="current(this)">Feedback</a></li>
</ul>
But I want the class to change whenever I click a different page as the "current" class highlights the link in the menu. How do I do this? I tried Javascript as shwon below but it doesn't work... thnx in advance
function current(page)
{
//alert(page);
document.getElementById("home").setAttribute("class", "");
document.getElementById("onlineSchedule").setAttribute("class", "");
document.getElementById("partners").setAttribute("class", "");
document.getElementById("services").setAttribute("class", "");
document.getElementById("products").setAttribute("class", "");
document.getElementById("newsHighlights").setAttribute("class", "");
document.getElementById("signup").setAttribute("class", "");
document.getElementById("feedback").setAttribute("class", "");
document.getElementById(page).className += "current";
var temp = document.getElementById("home").getAttribute("class");
alert(temp);
}
Upvotes: 0
Views: 356
Reputation: 2468
Here's an example of working code:
<style type="text/css">
.current {
color: red;
}
.current a {
color: green;
}
</style>
<div class="header box">
<ul class="nav box">
<li class="current" id="home"><a href="index.php" onclick="current('home')">Home</a></li>
<li class="" id="onlineSchedule"><a href="onlineSchedule.php" onclick="current(this)">Online Schedule</a></li>
<li class="" id="partners"><a href="partners.php" onclick="current(this)">Partners/Centers</a></li>
<li class="" id="services"><a href="services.php" onclick="current(this)">Services</a></li>
<li class="" id="products"><a href="products.php" onclick="current(this)">Products</a></li>
<li class="" id="newsHighlights"><a href="newsHighlights.php" onclick="current(this)">News/Highlights</a></li>
<li class="" id="signup"><a href="signup.php" onclick="current(this)">Sign Up!</a></li>
<li class="" id="feedback"><a href="feedback.php" onclick="current(this)">Feedback</a></li>
</ul>
<script type="text/javascript">
function current(page)
{
document.getElementById("home").setAttribute("class", "");
document.getElementById("onlineSchedule").setAttribute("class", "");
document.getElementById("partners").setAttribute("class", "");
document.getElementById("services").setAttribute("class", "");
document.getElementById("products").setAttribute("class", "");
document.getElementById("newsHighlights").setAttribute("class", "");
document.getElementById("signup").setAttribute("class", "");
document.getElementById("feedback").setAttribute("class", "");
document.getElementById(page).className = "current";
}
current("home");
</script>
I'm assuming you wanted to format the link as well, so I showed how to by adding in .current a { }
which is where you'll put CSS for the current link. .current { }
will mostly only affect the list item.
Upvotes: 1