Jump to content

Tabs & Current Tab


Wolphie

Recommended Posts

Ok, so i've created a tabbing system, but i need to add a style to the current tab that it's on.

 

This is my javascript:

	function initTabs(id) {
      var parent = document.getElementById("tab_parent");
      var elements = document.getElementsByTagName("div");
      var aelements = document.getElementsByTagName("a");
  var child, anchor;
  for(var i in elements) {
        if(elements[i].id == "tab_" + id) child = elements[i];
      }
      for(var i in aelements) {
   anchor = aelements[i];
       anchor.className = (anchor.id == child.id) ? "currentTab" : "";
      }
      if(child != null) parent.innerHTML = child.innerHTML;
    }

 

This is my HTML:

  <body>
    <a href="javascript:initTabs(0);" id="tab_0">Tab 1</a>
    <a href="javascript:initTabs(1);" id="tab_1">Tab 2</a>
    <a href="javascript:initTabs(2);" id="tab_2">Tab 3</a>
    <div id="tab_parent"></div>
    <div style="display: none;" id="tab_0">Yoooo this is tab 1</div>
    <div style="display: none;" id="tab_1">Yoooo this is tab 2</div>
    <div style="display: none;" id="tab_2">Yoooo this is tab 3</div>
    <script type="text/javascript">
    // <![CDATA[
      initTabs(0);
    // ]]>
    </script>
  </body>

 

 

However, the class name is only being added and removed from the 1st anchor tab, rather than being removed from the previous tab and added onto the current tab.

Can anybody help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.