Jump to content

tabbed menu to show divs and change class


adambedford

Recommended Posts

I'm making a tabbed menu that displays an inline div onclick. I've written this and got it to switch between divs when clicking a subsequent menu item but I am having a little trouble switching the class of the <li> tab onclick.

 

I know how to add a class to an element but the problem I'm having is that I don't know how to remove it when another tab is clicked and switch the class to that tab.

 

My menu script is as follows (it's probably quite cumbersome by professional standards but it works for me!)

 


function menudiv (layertoshow, layertohide)
{
  //Declare the variables	
  var show, hide, show_vis, hide_vis;
  
  //code for show layer
  if( document.getElementById ) // this is the way the standards work
    show = document.getElementById( layertoshow );
  else if( document.all ) // this is the way old msie versions work
      show = document.all[layertoshow];
  else if( document.layers ) // this is the way nn4 works
    show = document.layers[layertoshow];
  show_vis = show.style;
  
  //code for hide layer
  if( document.getElementById ) // this is the way the standards work
    hide = document.getElementById( layertohide );
  else if( document.all ) // this is the way old msie versions work
      hide = document.all[layertohide];
  else if( document.layers ) // this is the way nn4 works
    hide = document.layers[layertohide];
  hide_vis = hide.style;
  
  // if the style.display value is blank we try to figure it out here
  if(show_vis.display==''&&show.offsetWidth!=undefined&&show.offsetHeight!=undefined)
    show_vis.display = (show.offsetWidth!=0&&show.offsetHeight!=0)?'block':'none';
  show_vis.display = (show_vis.display==''||show_vis.display=='block')?'none':'block';
  
  
  if(hide_vis.display==''&&hide.offsetWidth!=undefined&&hide.offsetHeight!=undefined)
    hide_vis.display = (hide.offsetWidth!=0&&hide.offsetHeight!=0)?'none':'block';
  hide_vis.display = (hide_vis.display==''||hide_vis.display=='none')?'none':'none';
  

}

 

How would I go about integrating into the above script something that, when the tab is selected, adds the class "tab_selected" to the <li>. I guess I would need to pass the id of the <li> to the function but I don't know what to do from there so that when the next tab is selected, the previous one has the class removed.

 

I only have two tabs that I need to switch between, hence why I pass a 'tab to show' and a 'tab to hide' variable. I don't need it to work with more menu items.

 

Any thoughts?

Link to comment
Share on other sites

Without having looked too much into your code, are you aware of the className property? If you're only using a single class name for each element then it's extremely simple to switch between, but even with multiple it's just a case of some simple string substitution.

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.