473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Expand/collapse blocks of divs in a page

sam
Hi all,

I have a series of divs in 2 blocks say BLOCK1 and BLOCK2 and I want to
use one click to expand/collapse all those in each block. But the code
I came up with exapands all the divs in the entire page. How do I
restrict it to each block and also how do I cllapse those in each block
and change the text to Collapse All. I am stumped here.
Any help really really appreciated. Here is my code.

Thanks,
Sam
<html>
<head>
<script>
function expand_collapse()
{
var allDivs;
if (document.getElementsByTagName)
{ allDivs = document.getElementsByTagName('div'); }
else if (document.all)
{ allDivs = document.body.all.tags('div'); }
if (allDivs)
{
for (var x=0; x<allDivs.length; x++)
{ allDivs[x].style.display = 'block'; }
}
}
</script>
</head>

<body>
<div id="header" style="width: 550px; margin: auto;">

<div id="main1" style="border: 1px solid #ccc; padding: 10px;">
<b></b>BLOCK 1 </b>
<div style="text-align:right;"><a
href="javascript:expand_collapse();">Expand All</a></div>
<div class="title">
<h4> title 1</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>

<div class="title">
<h4> title 2</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
</div>
<br />

<div id="main2" style="border: 1px solid #ccc; padding: 10px;">
<b>BLOCK 2</b>
<div style="text-align:right;"><a
href="javascript:expand_collapse();">Expand All</a></div>
<div class="title">
<h4> title 1</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
<div class="title">
<h4> title 2</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
</div>

</div>
</body>
</html>

May 22 '06 #1
2 4636
sam wrote:
Hi all,

I have a series of divs in 2 blocks say BLOCK1 and BLOCK2 and I want to
use one click to expand/collapse all those in each block. But the code
I came up with exapands all the divs in the entire page. How do I
restrict it to each block and also how do I cllapse those in each block
and change the text to Collapse All. I am stumped here.
Any help really really appreciated. Here is my code.

Thanks,
Sam
<html>
<head>
<script>
function expand_collapse()
{
var allDivs;
if (document.getElementsByTagName)
{ allDivs = document.getElementsByTagName('div'); }
else if (document.all)
{ allDivs = document.body.all.tags('div'); }
if (allDivs)
{
for (var x=0; x<allDivs.length; x++)
{ allDivs[x].style.display = 'block'; }
}
}
</script>
</head>

<body>
<div id="header" style="width: 550px; margin: auto;">

<div id="main1" style="border: 1px solid #ccc; padding: 10px;">
<b></b>BLOCK 1 </b>
<div style="text-align:right;"><a
href="javascript:expand_collapse();">Expand All</a></div>
<div class="title">
<h4> title 1</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>

<div class="title">
<h4> title 2</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
</div>
<br />

<div id="main2" style="border: 1px solid #ccc; padding: 10px;">
<b>BLOCK 2</b>
<div style="text-align:right;"><a
href="javascript:expand_collapse();">Expand All</a></div>
<div class="title">
<h4> title 1</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
<div class="title">
<h4> title 2</h4>
</div>
<div style="display:none; border: 1px solid #CCC; padding: 20px;">
TEST TEST TEST TESt
</div>
</div>

</div>
</body>
</html>

Hi,

I think you can approach this a lot easier:

<span onClick="showHide('block','main1*main23*main27')"> Show</a>
<span onClick="showHide('none','main1*main23*main27')">H ide</a>

<div id="main1">I am main1</div>
<div id="main2">I am main2</div>
.....
<div id="main23">I am main23</div>
<div id="main27">I am main27</div>
<div id="main28">I am main28</div>

<script type="text/javascript">
function showHide(theDisplay,divsStr){
// get all div's
var theDivArr = divStr.split("*");
for(var i=0;i<theDivArr.length;i++){
document.getElementById(theDivArr[i]).style.display=theDisplay;
}
}
</script>

(Not tested.)

Regards,
Erwin Moller
May 22 '06 #2

sam wrote:
Hi all,

I have a series of divs in 2 blocks say BLOCK1 and BLOCK2 and I want to
use one click to expand/collapse all those in each block. But the code
I came up with exapands all the divs in the entire page. How do I
restrict it to each block and also how do I cllapse those in each block
and change the text to Collapse All. I am stumped here.
Any help really really appreciated. Here is my code.


Why not just do one of the following:
Use a naming convention, so that each block, and the subdivs can be
determined, so block a may have an id: blocka, and then just append a
number for each subdiv, so you have blocka1, blocka2, ..., blockan

Then, you can just open and close them as needed.

Or, if each div is the immediate child of the block div, then just get
all the childnodes for the block div, and open/close those.

May 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James Hurrell | last post by:
Hi, I'm using the following javascript function to expand and collapse portions of text in a web page (targeted at IE5.5 and above): function doExpand(paraNum,arrowNum) { if...
3
by: ireneatngs | last post by:
Hi, I have example html below which contains a couple of hidden divs. However, some of the table borders within these hidden divs are actually displayed when they should not be. In my...
7
by: peter | last post by:
I use the click-to-expand menu at http://javascript.internet.com/navigation/click-to-expand-menu.html This works fine, but is there a way to expand or collapse all the menus? An example of how...
1
by: Randy Starkey | last post by:
Hi, Is there a way to expand and collapse all if I have multiple implementations of this script on a single page? The script works well individually. Thanks! --Randy Starkey ---
0
by: Shadow Lynx | last post by:
When using ASP.NET 2.0's built-in TreeView on a page with <BASE target = "AnythingBut_Self"></BASE> in the HEAD, the expand/collapse buttons fail to function. The reason for this is that the...
0
by: jim | last post by:
Hi, I have a TreeView control that sits on the MasterPage. All of my other webpages inherit from that Master Page. The Treeview receives it's data using an XMLDataSource that has it's DataFile...
4
by: Rabel | last post by:
I am not very good at javascript I mostly am a flash developer but I am trying to apply one of our old expanding menus to work for a new site but it doesn't collapse the way I need it to right now...
0
by: Homer J. Simpson | last post by:
A few weeks ago I asked for suggestions on how to persist a tree's node state to cookies, without causing postbacks on each click in the tree. I had a single .aspx file, with a row of buttons on...
0
by: pbd22 | last post by:
Hi. I have two divs inside a containing div <div id=container> <div id=top> </div> <div id=bottom> </div> </div>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.