I am making some tabbed content, I have the tabs inline and overlapping each other slightly, meaning they have three states: inactive tab (“it” class), active tab (“at” class) and trailing tab (“tt” class) which follows the active tab. In the code sample below, tab three is active and tab four is the trailing tab as it comes after it.
<div id="tabnest">
<div id="tab" class="it"><a href="#" onclick="#">Tab One</a></div>
<div id="tab" class="it"><a href="#" onclick="#">Tab Two</a></div>
<div id="tab" class="at"><a href="#" onclick="#">Tab Three</a></div>
<div id="tab" class="tt"><a href="#" onclick="#">Tab Four</a></div>
<div id="tab" class="it"><a href="#" onclick="#">Tab Five</a></div><br>
</div>
<div id="content">
</div>
I’m a total newbie to JavaScript I’d like each tab when clicked to:
1. change all the other tabs classes to “it”,
2. change the current, clicked tab class to “at” and
3. change the next tabs class to “tt” if one exists.
From research on the net, I understand that these are the functions I want roughly:
document.getElementById('tab').nextSibling;
document.getElementById('tab').className = "it";
I just don’t know how to call a function and stich it up together. Sorry for the complicatedness of this, if it’s impossible to call the next div/tab and change its class solely then don’t worry, could you just help me change the selected tabs class and all the others please? Thanks in advance.
See http://jsfiddle.net/E7jnq/1/ for a jQuery implementation, or http://jsfiddle.net/ryMH3/ for a vanilla JS implementation.
jQuery:
Vanilla JS: