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
https://forums.phpfreaks.com/topic/98223-tabs-current-tab/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.