Jump to content

[SOLVED] multiple style.visibility set


ted_chou12

Recommended Posts

Hi, i am trying to set the visibility of multiple divs, so if the user clicks on one div, the rest hides, and only this one shows up, here is the js:

function setvisibility(id) {
var obj = document.getElementById(id);
if (obj.style.visibility == "hidden") {
var numcount = 1;
while (numcount <= 99) 
  {var obj2 = document.getElementById("tab" + numcount);
  obj2.style.visibility = "hidden"; numcount ++;}
  obj.style.visibility = "visible";
  } else {obj.style.visibility = "hidden";}
}

obj is the one that is clicked on, and obj2 is the rest looping through while, here is the tabs in html:

<li><a onclick="javascript: setvisibility('tab1');"><span><?php echo $type;?></span></a></li>
<li><a onclick="javascript: setvisibility('tab2');"><span><?php echo $type;?></span></a></li>
<li><a onclick="javascript: setvisibility('tab3');"><span><?php echo $type;?></span></a></li>
<li><a onclick="javascript: setvisibility('tab4');"><span><?php echo $type;?></span></a></li>
<li><a onclick="javascript: setvisibility('tab5');"><span><?php echo $type;?></span></a></li>
<div id=\"tab1\" class=\"tabs\">content1</div>
<div id=\"tab2\" class=\"tabs\">content1</div>
<div id=\"tab3\" class=\"tabs\">content1</div>
<div id=\"tab4\" class=\"tabs\">content1</div>
<div id=\"tab5\" class=\"tabs\">content1</div>

The problem is that nothing is showing up, and when clicking the link, nothing shows up as well.

I am using while to loop until 9 because I don't know how many tabs there will be, so im assuming the no of tabs dont exceed 99, if anyone knows how to get the number of tabs to a variable in js, that would help a lot as well.

Thanks,

Ted

Link to comment
Share on other sites

Change the onclick calls to only send the numerical part of the DIV ID. Then use the function shown below:

 

<html>
<head>
<script language="javascript">

function setvisibility(id)
{
    for (var tabIdx=1; tabObj=document.getElementById('tab'+tabIdx); tabIdx++) 
    {
        tabObj.style.visibility = (id==tabIdx) ? 'visible' : 'hidden';
    }
    return;
}


</script>
</head>
<body>

<li><a onclick="javascript: setvisibility(1);"><span>One</span></a></li>
<li><a onclick="javascript: setvisibility(2);"><span>Two</span></a></li>
<li><a onclick="javascript: setvisibility(3);"><span>Three</span></a></li>
<li><a onclick="javascript: setvisibility(4);"><span>Four</span></a></li>
<li><a onclick="javascript: setvisibility(5);"><span>Five</span></a></li>
<div id="tab1" class="tabs">content1</div>
<div id="tab2" class="tabs">content2</div>
<div id="tab3" class="tabs">content3</div>
<div id="tab4" class="tabs">content4</div>
<div id="tab5" class="tabs">content5</div>

<body>
</html>

Link to comment
Share on other sites

  • 2 months later...
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.