473,769 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed for a expand-collapse menu

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.getEle mentById('insid eSubCategory'+i );
sel.style.displ ay = 'none';
}
var sel = document.getEle mentById('insid eSubCategory'+n n);
sel.style.displ ay = 'block';
}

then the code for the text that pops up once the button is clicked is

<div id="insideSubCa tegory1" style="border:1 px; display:none;">
<div style="display: block; padding-left:3px;">
<table width="741" border="0">
<tr>
<td width="500" valign="top"><s pan
class="bodycopy "><b>Experience </b><br />
has eighteen years of planning and architectural experience. Since
joining </span></span><p><span class="bodycopy "><b>Educat ion</b><br />
Master of City Planning<br />
University of Pennsylvania, Certificate of Urban
Design</span></p>
<p class="bodycopy "><b>Licens es</b><br />
Registered Architect, PA, NJ, MD, NCARB Certified</p>
</span></td>
<td width="231" valign="top"><t able width="231" border="0"
bgcolor="#638BC F">
<tr>
<td width="125" bgcolor="#638BC F"><img src="Davirmann. jpg"
width="125" height="180" /></td>
<td width="96" bgcolor="#638BC F">&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</div>

so I need help with the collapse part and this is how I would like to
do it - add a graphic that is a "+" then a "-" when clicked on to
expand and collapse - im not sure how to make that graphic change and
for the expand or collapse code to be run. Any help is apprieciated

Thanks
Randy

Nov 3 '06 #1
4 8356
Ok, first thing you want to do is scrap your HTML. You should be using
UL/LI's for this, not what you've got now. You should do your
formatting with CSS, not using <br /tags (which in the long run will
only make life more difficult).

Hope this helps:

<html>
<head>
<title></title>
<style>
..menu ul { display: none; list-style-type: none; padding-left: 0; }
..menu ul li { border: 1px solid gray; cursor: pointer; }
/* this doesn't work on IE6, but who cares about IE6 anymore? ;) */
..menu ul li:hover { background-color: #ffffdd; }
div.menu { float: left; width: 200px; }
</style>
<script>
// Convenient wrapper for document.getEle mentById (who want to type
that all the time?)
function $(id) {
return document.getEle mentById(id);
}

// Recursively search an element's parents for the first parent with a
given tag name
function getParentByTag( element, tag) {
if (!element || element.tagName == 'HTML') return null;
if (element.tagNam e == tag) return element;
return getParentByTag( element.parentE lement || element.parentN ode,
tag);
}

// Toggles the menu - this is the actual event handler
function toggleMenu(e) {
e = e || window.event;
var tgt = e.target || e.srcElement;

// Get the actual element we put this handler on
// Not always the same as the event target - usually a parent
var div = getParentByTag( tgt, 'DIV');
if (!div) return; // Fail silently

// Assuming, of course, you've only got one UL in your div
var ul = div.getElements ByTagName('UL')[0];
hideAllMenus();
if (ul.style.displ ay == 'block') ul.style.displa y = 'none';
else ul.style.displa y = 'block';
if (e.stopPropogat ion) e.stopPropogati on();
else if (e.cancelBubble ) e.cancelBubble( );
}

// Hide all the menus in divs with class "menu"
function hideAllMenus() {
var divs = document.getEle mentsByTagName( 'DIV');
for (var i=0; i<divs.length; i++) {
// Go through all divs in the document looking for className
containing
// (not ==! - there can be multiple classes) "menu"
if (divs[i].className.sear ch('menu') -1) {
var ul = divs[i].getElementsByT agName('UL')[0];
ul.style.displa y = 'none';
}
}
}

// Make it happen in an onload handler
// Otherwise, this script gets executed before the DOM functions have a
document
window.onload = function() {
hideAllMenus();
var menuIds = ['menu1', 'menu2'];
for (var i=0; i<menuIds.lengt h; i++) {
$(menuIds[i]).onclick = toggleMenu;
}
}
</script>
<body>
<div id="menu1" class="menu">
<h3>About</h3>
<ul>
<li>About me</li>
<li>More about me</li>
<li>Lots more about me</li>
</ul>
</div>
<div id="menu2" class="menu">
<h3>Stuff</h3>
<ul>
<li>What I do</li>
<li>Give me money</li>
</ul>
</div>
</body>
</html>

-David

Rabel wrote:
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.getEle mentById('insid eSubCategory'+i );
sel.style.displ ay = 'none';
}
var sel = document.getEle mentById('insid eSubCategory'+n n);
sel.style.displ ay = 'block';
}

then the code for the text that pops up once the button is clicked is

<div id="insideSubCa tegory1" style="border:1 px; display:none;">
<div style="display: block; padding-left:3px;">
<table width="741" border="0">
<tr>
<td width="500" valign="top"><s pan
class="bodycopy "><b>Experience </b><br />
has eighteen years of planning and architectural experience. Since
joining </span></span><p><span class="bodycopy "><b>Educat ion</b><br />
Master of City Planning<br />
University of Pennsylvania, Certificate of Urban
Design</span></p>
<p class="bodycopy "><b>Licens es</b><br />
Registered Architect, PA, NJ, MD, NCARB Certified</p>
</span></td>
<td width="231" valign="top"><t able width="231" border="0"
bgcolor="#638BC F">
<tr>
<td width="125" bgcolor="#638BC F"><img src="Davirmann. jpg"
width="125" height="180" /></td>
<td width="96" bgcolor="#638BC F">&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</div>

so I need help with the collapse part and this is how I would like to
do it - add a graphic that is a "+" then a "-" when clicked on to
expand and collapse - im not sure how to make that graphic change and
for the expand or collapse code to be run. Any help is apprieciated

Thanks
Randy
Nov 3 '06 #2

David Golightly wrote:
Ok, first thing you want to do is scrap your HTML. You should be using
UL/LI's for this, not what you've got now. You should do your
formatting with CSS, not using <br /tags (which in the long run will
only make life more difficult).

Hope this helps:

<html>
<head>
<title></title>
<style>
.menu ul { display: none; list-style-type: none; padding-left: 0; }
.menu ul li { border: 1px solid gray; cursor: pointer; }
/* this doesn't work on IE6, but who cares about IE6 anymore? ;) */
.menu ul li:hover { background-color: #ffffdd; }
div.menu { float: left; width: 200px; }
</style>
<script>
// Convenient wrapper for document.getEle mentById (who want to type
that all the time?)
function $(id) {
return document.getEle mentById(id);
}

// Recursively search an element's parents for the first parent with a
given tag name
function getParentByTag( element, tag) {
if (!element || element.tagName == 'HTML') return null;
if (element.tagNam e == tag) return element;
return getParentByTag( element.parentE lement || element.parentN ode,
tag);
}

// Toggles the menu - this is the actual event handler
function toggleMenu(e) {
e = e || window.event;
var tgt = e.target || e.srcElement;

// Get the actual element we put this handler on
// Not always the same as the event target - usually a parent
var div = getParentByTag( tgt, 'DIV');
if (!div) return; // Fail silently

// Assuming, of course, you've only got one UL in your div
var ul = div.getElements ByTagName('UL')[0];
hideAllMenus();
if (ul.style.displ ay == 'block') ul.style.displa y = 'none';
else ul.style.displa y = 'block';
if (e.stopPropogat ion) e.stopPropogati on();
else if (e.cancelBubble ) e.cancelBubble( );
}

// Hide all the menus in divs with class "menu"
function hideAllMenus() {
var divs = document.getEle mentsByTagName( 'DIV');
for (var i=0; i<divs.length; i++) {
// Go through all divs in the document looking for className
containing
// (not ==! - there can be multiple classes) "menu"
if (divs[i].className.sear ch('menu') -1) {
var ul = divs[i].getElementsByT agName('UL')[0];
ul.style.displa y = 'none';
}
}
}

// Make it happen in an onload handler
// Otherwise, this script gets executed before the DOM functions have a
document
window.onload = function() {
hideAllMenus();
var menuIds = ['menu1', 'menu2'];
for (var i=0; i<menuIds.lengt h; i++) {
$(menuIds[i]).onclick = toggleMenu;
}
}
</script>
<body>
<div id="menu1" class="menu">
<h3>About</h3>
<ul>
<li>About me</li>
<li>More about me</li>
<li>Lots more about me</li>
</ul>
</div>
<div id="menu2" class="menu">
<h3>Stuff</h3>
<ul>
<li>What I do</li>
<li>Give me money</li>
</ul>
</div>
</body>
</html>

-David

Thanks David but I tried to create the graphic part I was talking about
for your code and couldnt do that could you add that code for me.

I wanted to add a graphic that is a "+" then a "-" when clicked on to
expand and collapse

Nov 6 '06 #3
anybody else have any ideas on how to make this work

Thanks

Nov 14 '06 #4
ASM
Rabel a écrit :
anybody else have any ideas on how to make this work

Thanks
Any other idea exept to use lists (ul -li -ul -li)
The history about [+] or [-] is very easy to fix.

example :

you make an image with [+][-]
both together side to side (size of each sign = 24/16px)

<html>
<style type="text/css">
#menu { list-style: none; width: 120px; margin:0;paddin g:0; }
#menu li, #menu ul { margin: 0; padding: 0; }
#menu li { background: url(image_on_of f.gif) no-repeat -24px top;}
#menu a { display: block; margin: 2px; margin-left: 26px;
background: #ffc; height: 20px;}
#menu li ul li { background-image: none; }
/* background image in slide doors */
#menu .hide { background-position: left top; }
#menu .hide ul { display: none; }
</style>
<script type="text/javascript">

function showHide(what) {
what.className = what.className= =''? 'hide' : '';
}

function init() {
var M = document.getEle mentById('menu' );
M = M.getElementsBy TagName('li');
for(var i=0; i<M.length; i++)
if(M[i].getElementsByT agName('ul').le ngth>0) {
showHide(M[i]);
M[i].onclick = function() { showHide(this); return false;}
}
}
onload = init;
</script>

<ul id="menu">
<li><a href="about.htm ">About</a>
<ul>
<li><a href="me.htm">A bout me</a></li>
<li><a href="job.htm"> My job</a></li>
</ul>
</li>
<li><a href="section_1/">Section 1</a>
<ul>
<li><a href="section_1/1.htm">Intro</a></li>
<li><a href="section_1/2.htm">History</a></li>
</ul>
</li>
</ul>
</html>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 15 '06 #5

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

Similar topics

1
2253
by: Pekka Niiranen | last post by:
Hi there, after reading TkInter/thread -recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 I wondered if it was possible to avoid using threads for the following problem: I have script started from W2K console that normally prints ascii messages to the screen. However, I have command line "debug" -flag that might cause printing
1
3191
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a i386-pc-solaris2.9. Seeing reference to gcc-2.7.2 in the Makefile of the code, I downloaded it and compiled in my home directory. Then changed the referenes of LIBDIR and INCLUDES to this installation .and ran with g++ for 2.7.2 then there are still...
5
8984
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need something more sophistictated like a vector of row objects. The data is of this format (same number of columns for each row): A, x, y, z, ...
4
1257
by: Patrick.O.Ige | last post by:
Hi All, Can anybody help in converting the code below to VB.NEt thanks. I need the loop converted.. thanks Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As TreeNodeCollection, ByVal expand As Boolean) Dim node As TreeNode 'Loop through each node of the collection and expand it. For Each node In nodes
2
1364
by: clsmith66 | last post by:
In a treeview, I am trying to store the index of every treenode as it expands in a hidden input box. If I click on the node itself, it expands and I can capture the node index no problem. If I click on the plus sign however, the selected index doesn't change so I can't capture the index of the newly expanded node. Does any one know how to change the selected node when a plus sign is clicked?
8
1685
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me a way to see that the data is in set by parenthesising it. This is all generated as I said using pexpect - Here is how I use it.. child = pexpect.spawn( _buildCadenceExe(), timeout=timeout) child.sendline("somefunction()")
10
1453
by: Lebowski | last post by:
Problem i have records like these: name quantity quality A 5 2.5 B 4 1 B 3 2 C 1 1.5 .... ... ...
7
1387
by: Lazareth | last post by:
Hi. Please help. Still learning... I am trying to write some code that will assign certain values to a variable based on values from a textbox. So far I have the following but It doesn't seen to be working. It should be that if my textbox is 0-3 then the expand value should be 0 4-6 then the expand value should be 1
1
2075
by: cbradio | last post by:
thescripts gurus, I would like to know if I could get some help with making elements of my layout expand (liquid) to the size of their containers, but also allow for some predefined, static margin/spacing. The layout is table-based, only because it needs to be very modular (going into more of an app than a website, and being reused) and needs to accommodate content of various sizes. Although I suppose i could mimic this with min-height...
1
1332
by: GolfinRover | last post by:
Using the Newton-Raphson method, find the two roots of the equation 3x^2 + 2x -2=0. (Hint: There is one positive root and one negative root.) (x+1) = x - (3x^2 +2x -2) / (6x +2) This is the equation given for the Newton-Raphson method. Expand|Select|Wrap|Line Numbers Head File #include <iostream> #include <cmath> using namespace std; #include "Newton.cpp"
0
9579
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
9422
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
10038
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
9987
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
8867
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...
0
5294
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...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.