472,789 Members | 1,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Script to collapse a list

I have a list built on HTML and CSS:

<ul>
<li>Foo</li>
<li>Bar
<ul>
<li>Gee</li>
</ul>
</li>
</ul>

I need a script to expand and collapse items so only one sublist is visible
at a time. I've found several scripts for trees but they either need the
list to be in their own format or they add tons of weird code to insert
icons and style the list (which breaks my design). Could you recommend me a
simple script to do so?
--
-- Álvaro G. Vicario - Burgos, Spain
-- Don't e-mail me your questions, post them to the group
--
Jul 23 '05 #1
4 2453
Alvaro G Vicario wrote:
I need a script to expand and collapse items so only one sublist is
visible at a time.


My tree code doesn't limit it to having one subtree open at a time, bu you
could write code to do so.
http://www.javascripttoolbox.com/mktree/

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #2
Alvaro G Vicario wrote:
I have a list built on HTML and CSS:

<ul>
<li>Foo</li>
<li>Bar
<ul>
<li>Gee</li>
</ul>
</li>
</ul>

I need a script to expand and collapse items so only one sublist is visible
at a time. I've found several scripts for trees but they either need the
list to be in their own format or they add tons of weird code to insert
icons and style the list (which breaks my design). Could you recommend me a
simple script to do so?


The following is about as simple as possible, the onclick
handler is added dynamically, so there is no script in the HTML.

If JavaScript is not enabled, the menus appear in full - i.e.
not collapsed. Lightly commented.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>Collapsing Menu Example</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<meta name="Date" content="2005-02-15">
<meta name="Author" content="Fred Oz">
<style type="text/css">
..level_1, .level_2, .level_3, .level_4 {
cursor:pointer;
cursor:hand;
font-weight:normal;
list-style-image:url(plus.gif);
color:#003366;
padding-bottom:5px;
}

LI { padding-left:2px;}
a:visited {color: #003366;}
a:hover {color: #663333;}
a:active {color: #003366;}
a:link {color: #003366;}
..style1 {font-size: 14pt; font-weight: bold;}
body {margin-left: 15px;}
</style>

<script type="text/javascript">

// Handle menu click
function mClick(n) {

// Go up to LI or UL, whichever comes first
// Will always be LI except on load when will be UL
while (n.nodeName != 'LI' && n.nodeName != 'UL') {
n = n.parentNode;
}

// remember node
var n0 = n;

// Go up to UL if stopped at LI above
while (n.nodeName != 'UL') {
n = n.parentNode;
}

// Make children of all sibling UL's hidden
// Do not hide kids of node clicked on
// to prevent flashing off & on
var o = n.childNodes;
for (var i=0; i<o.length; ++i) {
if (0[i] != n0) hideUL(o[i]);
}

// Make children of node clicked on visible
for (var k=0; k<n0.childNodes.length; ++k) {
if (n0.childNodes[k].style) {
n0.childNodes[k].style.display='';
}
}
}

function hideUL(x) {
// Hide UL
if (x.nodeName == 'UL') x.style.display='none';

// Recurse down tree, hiding all ULs
for (var j=0; j<x.childNodes.length; ++j) {
hideUL(x.childNodes[j]);
}
}

// Not really needed, see note below
function resetMenu() {
mClick(document.getElementById("mainmenu"));
}

function initMenu(m){
var allAs=document.getElementById(m).getElementsByTagN ame('a');
var i = allAs.length;
while (i--) {
if (allAs[i].href == '#') {
allAs[i].onclick = function() {
mClick(this); return false;
};
}
}
}

</script>

</head>
<body onclick="initMenu('mainmenu');">
<!-- Menus start here -->
<ul id="mainmenu">
<li class="level_1"><a href="#">Menu 1</a>
<ul class="folderheader">
<li class="level_2"><a href="#">Report Types</a>
<ul class="folderheader">
<li class="level_3"><a href="#">Reports 1</a>
<ul class="folderheader">
<li class="level_4"><a href="#">Report Name</a>
<ul class="folderlist">
<li><a href="http://www.apple.com">Add New
Record</a></li>
<li><a href="http://www.yahoo.com">Edit
Records</a></li>
<li><a href="http://www.google.com">Clone
Records</a></li>
</ul>
</li>
<li class="level_4"><a href="#">Report Name</a>
<ul class="folderlist">
<li><a href="http://www.sun.com">Add New
Record</a></li>
<li><a href="http://www.ibm.com">Edit
Records</a></li>
<li><a href="http://www.news.com">Mass
Update</a></li>
<li><a href="http://www.kontraband.com">Clone
Records</a></li>
</ul>
</li>
</ul>
</li>
<li class="level_3"><a href="#">Reports 2</a>
<ul class="folderheader">
<li class="level_4"><a href="#">Report Name</a>
<ul class="folderlist">
<li>Add New Record</li>
<li>Edit Records</li>
<li>Mass Update</li>
<li>Clone Records</li>
</ul>
</li>
<li class="level_4"><a href="#">Report Name</a>
<ul class="folderlist">
<li>Add New Record</li>
<li>Edit Records</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="level_1"><a href="#">Menu 2</a>
<ul class="folderheader">
<li class="level_2"><a href="#">Report Name</a>
<ul class="folderlist">
<li>Add New Record</li>
<li>Edit Records</li>
</ul>
</li>
</ul>
</li>
<li><a href="#" onclick="resetMenu();">Close menus</a></li>
</ul>
<!-- Hide menus -->
<!--
If not intending to reset menus when link clicked
put the reset code here and get rid of resetMenu()
-->
<script type="text/javascript">
resetMenu();
</script>
<!-- Menus end here, put content below -->
</body>
</html>

--
Fred
Jul 23 '05 #3
In article <7f****************************@40tude.net>,
al******************@telecomputeronline.com enlightened us with...
I have a list built on HTML and CSS:

<ul>
<li>Foo</li>
<li>Bar
<ul>
<li>Gee</li>
</ul>
</li>
</ul>

I need a script to expand and collapse items so only one sublist is visible
at a time.


Will this do?

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function showHide(id)
{
if (document.getElementById)
{
if (document.getElementById(id).style.visibility == "visible"
|| document.getElementById(id).style.visibility == "")
{
// hide it
document.getElementById(id).style.visibility = "hidden";
document.getElementById(id).style.display = "none";
}
else
{
// show it
document.getElementById(id).style.visibility = "visible";
document.getElementById(id).style.display = "";
}
}
}
</script>
</head>

<body>
<p>This is a test. Click on high-level LI to hide sub-LI. Click again to
show.</p>
<ul>
<li><a onClick="showHide('tier1')";>one</a></li>
<ul id="tier1">
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>
<li><a onClick="showHide('tier2')";>two</a></li>
<ul id="tier2">
<li>ddd</li>
<li>eee</li>
<li>fff</li>
</ul>
<li><a onClick="showHide('tier3')";>three</a></li>
<ul id="tier3">
<li>ggg</li>
<li>hhh</li>
<li>iii</li>
</ul>
</ul>
</body>
</html>

--
--
~kaeli~
Have you forgotten about Jesus? Isn't it about time you did?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
*** Matt Kruse wrote/escribió (Tue, 15 Mar 2005 07:06:54 -0600):
Alvaro G Vicario wrote:
I need a script to expand and collapse items so only one sublist is
visible at a time.


My tree code doesn't limit it to having one subtree open at a time, bu you
could write code to do so.
http://www.javascripttoolbox.com/mktree/


Is there a quick way to allow image tags? My list, unfortunately, is
composed of images and if you click on them the expand/collapse action is
not triggered.
--
-- Álvaro G. Vicario - Burgos, Spain
-- Don't e-mail me your questions, post them to the group
--
Jul 23 '05 #5

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

Similar topics

15
by: greenflame | last post by:
First of all I only have IE for testing. Ok. I have a script that is supposed to show, not evaluate, the indefinite integral of something. When I run the script it just craches IE. I have tried...
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 ---
16
by: Barbara de Zoete | last post by:
Here's what I'm trying to do: Create a table with generic style property . Have a few table cells in the thead that 'have to' melt into eachother, so needing the style . Looking somthing...
5
by: sandipon | last post by:
I have used the following Pagination script to display database records in a series of pages, but only the first page shows up properly, and on clicking the links of subsequent pages or the NEXT ,...
5
by: YaoBao | last post by:
Is any ColdFusion script I can put on my webpage that will create a search bar so people can type keywords to match it on the current page in my website? It will be exactly like the finder search bar...
5
by: camphor | last post by:
hi, I have found an upload script in hotscripts and have implemented it into the website, I followed the installation steps to 'give write permissions to php on the upload folder (which is...
1
by: rfr | last post by:
Apparently the Transitional Doctype kills this script because the script does not make proper use of units like "px". It works well without a doctype statement. But once someone adds a...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.