Code (Text): <html> <head> <title>Website</title> <style type="text/css"> * { padding: none; margin: none; } body { cursor: default; } ul.nav { color: #333; font: 9px Georgia ; padding: 0px; } .nav li { display: inline; list-style-type: none; } .nav li a { color: #333; text-decoration: none; } .nav li a:hover { color: #555; font-weight: bold; } .nav li a.active { color: #555; font-weight: bold; } .nav li a.active:hover { color: #555; font-style: normal; font-weight: bold; } </style> </head> <body> <ul class="nav"> <li><a class="active" href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Works</a></li> <li><a href="#">Contact</a></li> </ul> </body> </html> There's the code. So I get a page with 4 links in li tags horizontally. What I want to do is when I click let's say "Works", make the link change it's class to "active" and change the link "Home" to have a not active class. So how do I achieve this? I don't mind if you either make the list change it's class or make the link stay in it's a:active state until another link is clicked. Thanks :kiss:
First set the the id of each <li> to something. Then the Javascript to change the class is Code (Text): document.getElementById(idhere).className = "newclassname";
thanks Zero. but that is to change class only. is there a way to get the links to return to normal state when I click another, I can't have all links in a "selected" state. Not very good at javascript. Anyone knows how to work this? Looping and such i think
Replace your HTML Code (Text): <ul class="nav"> <li><a class="active" href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Works</a></li> <li><a href="#">Contact</a></li> </ul> with this: Code (Text): <ul class="nav" id="nav"> <li><a class="active" href="#" onclick="setNav(this);">Home</a></li> <li><a href="#" onclick="setNav(this);">About</a></li> <li><a href="#" onclick="setNav(this);">Works</a></li> <li><a href="#" onclick="setNav(this);">Contact</a></li> </ul> and use this javascript function: Code (Text): function setNav(obj) { navLinks = document.getElementById('nav').getElementsByTagName('a'); for (i = 0; i < navLinks.length; i++) { if (navLinks[i] == obj) { obj.className = 'active'; } else { navLinks[i].className = ''; } } return true; } It should work