Unstoppable Domains

Javascript Help Needed!

Spaceship Spaceship
Watch

Nathan

Carpe DiemVIP Member
Impact
61
Hello all. I need a bit of help with some JS code. I don't really know it all that well, just enough to get by. Here's what I need. Basically I want a vertical tree menu that onclick shows sub items and onclick again it hides them. Also I want a little plus button and minus button to toggle when the list shows/hides. I've been attempting to do this from my limited knowledge, but it is not working properly.

Here's what I have:

In head:
Code:
<script type="text/javascript">
<!--
window.onload=show;
function show(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('category'+i)) {document.getElementById('category'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}
//-->
</script>

In body:
Code:
<dt onclick="javascript:show('category1');"><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/plusbutton.gif" onclick="this.src='<?php bloginfo('template_url'); ?>/images/minusbutton.gif'" border=0 /> Category 1</a></dt>
		<dd id="category1">
				<ul>
					<li><a href="#">  -Page 1</a></li>
					<li><a href="#">  -Page 2</a></li>
					<li><a href="#">  -Page 3</a></li>
				</ul>
		</dd>
</dl>

As you can see, I am not really sure about how to "hide" everything and how to toggle the image. Any help is very appreciated! :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
I usually hide elements with

Code:
document.getElementById('element').style.display = 'none';

To have an element originally hidden, start with

Code:
<div style='display: none;' id='element'>Content</div>

So some sample code:

Code:
<script type='text/javascript'>
function show(obj, elId) {
  var element = document.getElementById(elId);
  if (element.style.display == 'none') {
    obj.innerHTML = "-";
    element.style.display = '';
  }
  else {
    obj.innerHTML = "+";
    element.style.display = 'none';
  }
}
</script>

Code:
<div style='width: 100px; text-align: center; font-size: 18px; clear: both; float: left;' onclick="show(this, 'element1');">-</div>
<div id='element1'>
  [Content1]
</div>

<div style='width: 100px; text-align: center; font-size: 18px; float: left;' onclick="show(this, 'element2');">+</div>
<div id='element2' style='display: none;'>  
  [Content2]
</div>

There are certainly many ways to do this, but this is the way I know the best. You could easily condense the JavaScript a little bit, but I wanted you to be able to understand it.

I tested this on FireFox and it seems to work fine (Minus the style part of the menu). However, it is untested in Internet Explorer (Which is going to be your major problem as far as browser cooperation goes).


Bruce
 
1
•••
Thanks for the reply Bruce, I'd like to see another way to do this, as I cannot seem to get this one to work too well.
 
0
•••
I modified your original code and got it working just fine.
I threw in some html, head, and body tags. You'll probably want to change them to be valid xhtml or whatnot.

I also had to make the php function, I'm sure yours is much different.

Code:
<?php
function bloginfo($i) {
	echo $i; 
}
?>

<html>
<head>
<script type="text/javascript">

function show(id) {
	var d = document.getElementById(id), img = document.getElementById(id + '_img');
	if (d.style.display == 'none') {
		img.src = '<?php bloginfo('template_url'); ?>/images/minusbutton.gif';
		d.style.display = '';
	}
	else {
		img.src = '<?php bloginfo('template_url'); ?>/images/buttonplus.gif';
		d.style.display = 'none';
	}
}
</script>
</head>
<body>
<dl>
<dt onclick="show('category1');"><img src="<?php bloginfo('template_url'); ?>/images/plusbutton.gif" onclick="this.src='<?php bloginfo('template_url'); ?>/images/minusbutton.gif'" border=0 id='category1_img' /> Category 1</dt>
		<dd id="category1" style='display: none;'>
				<ul>
					<li><a href="#">  -Page 1</a></li>
					<li><a href="#">  -Page 2</a></li>
					<li><a href="#">  -Page 3</a></li>
				</ul>
		</dd>
</dl>
</body>
</html>

If you still need help, either post the link here or PM it to me, and I'll take a look at the direct code.


Bruce
 
0
•••
Thank you very much bearruler. This is precisely what I needed! +rep :)
 
0
•••

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back