Forum Moderators: open
<div id="tabs">
<ul>
<li><a class="active" href="1">Home</a></li>
<li><a href="2">Journal</a></li>
<li><a href="3">Pictures</a></li>
</ul>
</div>
So basically i need a way to switch the active class between the tabs depending on what page is loaded. I could probably do this in php, but im thinking javascript might be easier.
Anyone have any idea how to do it?
function setActiveTab(tabID) {
var currTabElem = document.getElementById(tabID);
currTabElem.setAttribute("class", "some_class_name");
currTabElem.setAttribute("className", "some_class_name");
return;
}
The second variant where the className is set is to cater for IE, which for reasons best known to MS does not recognise the class attribute.
You can also do this without giving the li elements ids if you prefer: then you will need to access the parent div (id='tabs') by ID, and traverse the DOM tree to the descendent li elements. Each of these can then be identified by their href values.
You would need to call this function with the appropriate argument on document load - I'm not sure from what you say in your post if this is a problem, but if so you could do something tricky like determine the active tab by looking at the value of the h1/title elem or something like that.
Hope this helps.
-b