473,809 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEl ementsByTagName )
{ allDivs = document.getEle mentsByTagName( 'div'); }
else if (document.all)
{ allDivs = document.body.a ll.tags('div'); }
if (allDivs)
{
for (var x=0; x<allDivs.lengt h; 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="javascrip t:expand_collap se();">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="javascrip t:expand_collap se();">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 4668
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.getEl ementsByTagName )
{ allDivs = document.getEle mentsByTagName( 'div'); }
else if (document.all)
{ allDivs = document.body.a ll.tags('div'); }
if (allDivs)
{
for (var x=0; x<allDivs.lengt h; 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="javascrip t:expand_collap se();">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="javascrip t:expand_collap se();">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="showHi de('block','mai n1*main23*main2 7')">Show</a>
<span onClick="showHi de('none','main 1*main23*main27 ')">Hide</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(theDis play,divsStr){
// get all div's
var theDivArr = divStr.split("* ");
for(var i=0;i<theDivArr .length;i++){
document.getEle mentById(theDiv Arr[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
22597
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 (paraNum.style.display=="none") { paraNum.style.display=""; arrowNum.src="collapse.gif"; }
3
1068
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 example, I have comments indicating that if I remove the 'border-collapse' attribute for two of the tables within the hidden divs, my problem is fixed.
7
17226
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 I use this is here: http://www.cod.edu/people/faculty/chenpe/PRAIRIE/index.html Thanks!
1
3359
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
2562
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 hyperlinks generated for the expand/collapse buttons includes javascript code that will execute in the frame specified in the Base Target rather than the current document. To remedy this, TreeView should render a target="_self" attribute for each...
0
2428
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 property set to the Web.sitemap file. I have turned off the Expand/Collapse icons on the Treeview. I want to be able to single click a node in the tree view to
4
8362
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 the code I am using looks like this function openSubCategory(n, nn) { var i = 0 for(i=1;i<n+1;i++) { var sel = document.getElementById('insideSubCategory'+i); sel.style.display = 'none';
0
2101
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 top of the screen, and a tree control below (with EnableViewState set to true). I let the user expand/collapse away to his hearts content without needless roundtrips. When the user clicked on a button at the top, I'd reload the page, check...
0
2475
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>
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.