Solved

hide/show nested divs

Posted on 2008-06-14
6
905 Views
Last Modified: 2010-04-21
hi,
how can I hide and show nested divs in <div id=main>  ?
<div id=main>
    <div>...</div>
    <div>...</div>
    <div>...</div>
</div>

Open in new window

0
Comment
Question by:uk1900
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21786013
try:
<script type="text/javascript"><!--
function toggle(i)
{
	var div = document.getElementById(i);
	if( div.style.display=='none')
		div.style.display=''
	else
		div.style.display='none'
}
//--></script>
<span onclick="toggle('main')">Toggle Main</span>
<div id='main' style='display:none'>
    <div>...a</div>
    <div>...b</div>
    <div>...c</div>
</div>

Open in new window

0
 
LVL 3

Expert Comment

by:Masoudgh
ID: 21786018
you must set "display" attribute. for example use a function like this:

<script type="text/javascript" language="javascript">
function SwitchVisibility(eID)
{
if (document.getElementById(eID).style.display=="none")
    document.getElementById(eID).style.display="";
else
    document.getElementById(eID).style.display="none";
}
</script>

and for change the display mode for any element you must call the function with id's of your element.
for example :
<div id="MainDiv">
      <div id="Div1" style="display:none">
            11111111111111111111111111111111111111111111111111111111</br>
      </div>
      <div id="Div2" style="display:none">
            22222222222222222222222222222222222222222222222222222222</br>
      </div>
      <div id="Div3" style="display:none">
            33333333333333333333333333333333333333333333333333333333</br>
      </div>
</div>
<span onclick="SwitchVisibility('Div2');">click here</span>
0
 

Author Comment

by:uk1900
ID: 21786042
thx hielo,
this will hide the main div too, is it possible to show/hide only the nested divs?


<div id='main' style='display:none'>
    <span onclick="ShowHideNested('main')">Toggle Main</span>
    <div>...a</div>
    <div>...b</div>
    <div>...c</div>
</div>

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21786097
see attached code
<script type="text/javascript"><!--
function toggle(i)
{
	var div = document.getElementById(i).getElementsByTagName("div");
	for( var i=0; i < div.length; ++i)
	{
		if( div[i].style.display=='none')
			div[i].style.display=''
		else
			div[i].style.display='none'
	}
}
//--></script>
 
<div id='main'>
    <span onclick="toggle('main')">Toggle Main</span>
    <div>...a</div>
    <div>...b</div>
    <div>...c</div>
 
</div>

Open in new window

0
 

Author Closing Comment

by:uk1900
ID: 31467219
thank you for your help
0
 
LVL 82

Expert Comment

by:hielo
ID: 21786169
You are welcome!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question