yourbudweiser
asked on
Javascript Menu Tree Problem
I am using the following code to create a Menu Tree for my website. The problem is that when first loaded, it takes 2 clicks to expand. Any ideas? Any better coding ideas? Thanks alot!
<style>
body{
font: 10pt Verdana,sans-serif;
color: navy;
}
.text {
font-family: Verdana;
font-size: 10px;
}
.trigger{
cursor: pointer;
cursor: hand;
}
.branch{
display: none;
margin-left: 12px;
}
</style>
<script>
function Toggle(node)
{
// Unfold the branch if it isn't visible
if (node.nextSibling.style.di splay == 'none')
{
// Change the image (if there is an image)
if (node.childNodes.length > 0)
{
if (node.childNodes.item(0).n odeName == "IMG")
{
node.childNodes.item(0).sr c = "NOLINES_minus.gif";
}
}
node.nextSibling.style.dis play = 'block';
}
// Collapse the branch if it IS visible
else
{
// Change the image (if there is an image)
if (node.childNodes.length > 0)
{
if (node.childNodes.item(0).n odeName == "IMG")
{
node.childNodes.item(0).sr c = "NOLINES_plus.gif";
}
}
node.nextSibling.style.dis play = 'none';
}
}
</script>
<div class="trigger" onClick="Toggle(this)"><im g src="nolines_plus.gif" border="0"> Click Me</div>
<span class="branch">
<img src="joinbottom.gif"><a href="123.com">Coverage Modified</a><br>
<img src="joinbottom.gif"><a href="123.com">Driver Added</a><br>
<img src="joinbottom.gif"><a href="123.com">Vehicle Added</a><br>
</span>
<style>
body{
font: 10pt Verdana,sans-serif;
color: navy;
}
.text {
font-family: Verdana;
font-size: 10px;
}
.trigger{
cursor: pointer;
cursor: hand;
}
.branch{
display: none;
margin-left: 12px;
}
</style>
<script>
function Toggle(node)
{
// Unfold the branch if it isn't visible
if (node.nextSibling.style.di
{
// Change the image (if there is an image)
if (node.childNodes.length > 0)
{
if (node.childNodes.item(0).n
{
node.childNodes.item(0).sr
}
}
node.nextSibling.style.dis
}
// Collapse the branch if it IS visible
else
{
// Change the image (if there is an image)
if (node.childNodes.length > 0)
{
if (node.childNodes.item(0).n
{
node.childNodes.item(0).sr
}
}
node.nextSibling.style.dis
}
}
</script>
<div class="trigger" onClick="Toggle(this)"><im
<span class="branch">
<img src="joinbottom.gif"><a href="123.com">Coverage Modified</a><br>
<img src="joinbottom.gif"><a href="123.com">Driver Added</a><br>
<img src="joinbottom.gif"><a href="123.com">Vehicle Added</a><br>
</span>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey guys thanks for responding. Dakyd, although your script works perfectly as above, it doesn't work in the context of my page. I have alot of content between the trigger and branch. I have modified my original script and it now works perfectly. I appreciate your help, thanks!
I object in this case, as the solution provided by me solved the problem (at least with the example that was given).
As the questioner said he modified his script (which way? maybe by the way I mentioned?). He might post his solution so this question can be closed with
answerd himself refund the points
As the questioner said he modified his script (which way? maybe by the way I mentioned?). He might post his solution so this question can be closed with
answerd himself refund the points
ASKER
Hernst42 is right. For some reason, I didn't properly test his solution.
Thanks!
Thanks!
And while we're talking about re-writing ... will something like the following help? Your question reminded me of the suckerfish drop downs, so I rewrote what you had with the help of some CSS. You essentially use lists to create your menu system, and then style the lists to make them look the way you want. The javascript essentially just switches class names, letting the CSS do the rest. It should work the same cross-browser, and also make your life easier by eliminating the need to walk the DOM. Hope it helps.
<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style>
body{
font: 10pt Verdana,sans-serif;
color: navy;
}
.text {
font-family: Verdana;
font-size: 10px;
}
#nav, #nav ul {
padding: 0;
margin: 0;
list-style: none;
}
#nav li {
cursor: pointer;
cursor: hand;
display: block;
}
#nav li ul {
display: none;
margin-left: 12px;
}
#nav li.show ul {
display: block;
}
</style>
<script type="text/javascript">
window.onload = initMenu;
function initMenu()
{
var lis = document.getElementById("n
for (var i = 0, n = lis.length; i < n; i ++)
{
lis[i].onclick = toggle;
}
}
function toggle()
{
var imgs = this.getElementsByTagName(
if (this.className.indexOf("s
{
this.className = this.className.replace(/\s
if (imgs.length > 0)
imgs[0].src = "NOLINES_plus.gif";
}
else
{
this.className += " show";
if (imgs.length > 0)
imgs[0].src = "NOLINES_minus.gif";
}
}
</script>
</head>
<body>
<ul id="nav">
<li><img src="nolines_plus.gif" style="border: 0;" />Click Me
<ul>
<li><img src="joinbottom.gif"><a href="123.com">Coverage Modified</a></li>
<li><img src="joinbottom.gif"><a href="123.com">Driver Added</a></li>
<li><img src="joinbottom.gif"><a href="123.com">Vehicle Added</a></li>
</ul>
</li>
</ul>
</body>
</html>