/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How to make collapsable code appear collapsed on open

I have the code below that gives the ability to open and collapse sections. I don’t know too much javascript and found this code online. I tweaked it to serve my purposes but it doesn’t do one thing. I would like all of the areas to be collapsed when the page is opened. How would I tweak the code below to do that?

[code]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
<title>Untitled Page</title>
<script type=”text/javascript”>
function ExpandCollapseTable1(titleRow)
{
if(titleRow.parentNode.childNodes.length > 1)
{
if(titleRow.parentNode.childNodes[1].style.display==”block” || titleRow.parentNode.childNodes[1].style.display==””)
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “none”;
}
}
else
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “block”;
}
}
}
}
function ExpandCollapseTable2(titleRow)
{
if(titleRow.parentNode.childNodes.length > 1)
{
if(titleRow.parentNode.childNodes[1].style.display==”block” || titleRow.parentNode.childNodes[1].style.display==””)
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “none”;
}
}
else
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “block”;
}
}
}
}
function ExpandCollapseTable3(titleRow)
{
if(titleRow.parentNode.childNodes.length > 1)
{
if(titleRow.parentNode.childNodes[1].style.display==”block” || titleRow.parentNode.childNodes[1].style.display==””)
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “none”;
}
}
else
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “block”;
}
}
}
}
function ExpandCollapseTable4(titleRow)
{
if(titleRow.parentNode.childNodes.length > 1)
{
if(titleRow.parentNode.childNodes[1].style.display==”block” || titleRow.parentNode.childNodes[1].style.display==””)
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “none”;
}
}
else
{
for(var i=1;i<titleRow.parentNode.childNodes.length;i++)
{
titleRow.parentNode.childNodes[i].style.display = “block”;
}
}
}
}
</script>
</head>
<body>
<table border=’0′ bordercolor=’#000000′ width=’100%’ cellspacing=’0′ cellpadding=’0′ bgcolor=red>
<tr onclick=”ExpandCollapseTable1(this);” style=’cursor:pointer;’>
<td>&nbsp;&nbsp;I</td>
</tr>
<tr>
<td><table border=’0′ bordercolor=’#000000′ width=’100%’ cellspacing=’0′ cellpadding=’0′ bgcolor=lightgreen>
<tr onclick=”ExpandCollapseTable2(this);” style=’cursor:pointer;’>
<td>&nbsp;&nbsp;&nbsp;&nbsp;A</td>
</tr>
<tr>
<td><table border=’0′ bordercolor=’#000000′ width=’100%’ cellspacing=’0′ cellpadding=’0′ bgcolor=yellow>
<tr onclick=”ExpandCollapseTable3(this);” style=’cursor:pointer;’>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</td>
</tr>
<tr>
<td><table border=’0′ bordercolor=’#000000′ width=’100%’ cellspacing=’0′ cellpadding=’0′ bgcolor=lightblue>
<tr onclick=”ExpandCollapseTable4(this);” style=’cursor:pointer;’>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a</td>
</tr>
<tr>
<td bgcolor=orange>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTENT CONTENT CONTENT</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>[/code]

FYI, I’ve also posted this question at:
[url]http://www.tek-tips.com/submitpost.cfm?pid=216[/url]
[url]http://codingforums.com/showthread.php?p=980260[/url]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@tirnaAug 06.2010 — Your code is very cumbersome and messy to maintain imo because you have 4 very similar functions doing what you could do with a single function by passing an element id to it and you have nested tables in your html :eek:

Putting that aside, what you want to do should be easy by simply giving the appropriate elements a class name and then setting the display style for that class to "none".

But then when everything is collapsed (ie....hidden) what would you then click to expand the collapsed elements?
Copy linkTweet thisAlerts:
@DKYauthorAug 06.2010 — I'm looking to only see the red cell when I open the page.


Then when I click on the red row it expands to show me the lightgreen row,

then when I click on the lightgreen row it expands to show me the yellow row,

then when I click on the yellow row it expands to show me the lightblue row

and finally when I click on the lightblue row it expands to show me the orange row.

I have no idea how to do what you suggested above but would that give me what I'm looking for?
Copy linkTweet thisAlerts:
@DKYauthorAug 06.2010 — This looks like it will work for what I'm trying to do. Thanks!

http://www.dynamicdrive.com/dynamicindex1/treeview/index.htm
×

Success!

Help @DKY spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...