Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I have small problem that I have one main div and in that there are many small divs all are set display=none at page load. Later I will click on buttons then based on the button I have to open a div.

I have used the toggle function to hide and show a div but when I click Button2 the first div is still in show() Now I want that when I click a button only that corresponding div has to open and all other div's must be closed or hide.

Is it possible with Toggle or some other else?

Please do tell me some code for this.

Thanks in Advance
Ganesh
Posted

HTML
<html>
<head>

<script>
//this function will show the content within the <div> tag
function toggleDiv(divName) 
{
  thisDiv = document.getElementById(divName);
  if (thisDiv) 
  {
    if (thisDiv.style.display == "none") {
      thisDiv.style.display = "block";
    }
  }
  else {
    alert("Error: Could not locate div with id: " + divName);
  }
}

//this function will hide the content within the <div> tag
function toggleDiv2(divName) 
{
  thisDiv = document.getElementById(divName);
  if (thisDiv) 
  {
    if (thisDiv.style.display == "block") {
      thisDiv.style.display = "none";
    }
  }
  else {
    alert("Error: Could not locate div with id: " + divName);
  }
}
</script>
</head>

<body>
<table>
<tr>
<td>
<!-- calls the 'toggleDiv' that shows the <div> -->
<input name="ff1" type=radio value=Y onClick="toggleDiv('showme');">Show
<!-- calls the 'toggleDiv2' that hides the <div> -->
<input name="ff1" type=radio value=N onClick="toggleDiv2('showme');">Hide
</td>
</tr>
<tr>
<td>
<!-- by defualt hide the <div> tag -->
<div id="showme" style="display: none;">
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td><div align="right">Now you can see me, yeay!!</div></td>
<td> </td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
Some other text here
</td>
</tr>
</table>
</body>
</html>


</div></div>
 
Share this answer
 
If you want more finer control then on button click remove display none class only from the div you need to display. You can be sure using class selector to assign display none class to all the other divs.
 
Share this answer
 
You can try with Show() and Hide() in JQuery.
or refer the below link, which is same.

http://jquery.bassistance.de/accordion/demo/[^]

Still any issues, you can share your coding.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900