Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swap plus/minus images in collapsable menu

Status
Not open for further replies.

karren

Programmer
Feb 26, 2002
42
CA
hello all,

i am trying to make a very simple show/hide menu where some content is shown or hidden by clicking. i've got the show/hide piece to work, but i cannot get the image to swap from plus to minus when the user clicks on the plus image to expand the menu.

here is my javascript code:

<script type="text/javascript">
<!--
var counter=0;
var plusminus
function hide(el)
{
if(counter==0)
{
document.getElementById(el).style.display="block";
counter=1;
plusminus="minus";
}
else
{
document.getElementById(el).style.display="none";
counter=0;
plusminus="plus";
}

}

//-->
</script>


here is the html that calls it:

<a href="#" onClick="javascript: hide('menu1')"><script type="text/javascript">document.write('<img src="images/'+plusminus+'.jpg" id="plus" border="0">')</script>


i am not that familiar with javascript (just a beginner at the moment!). any help will be greatly appreciated!!

thanks so much in advance!!

karren



 
Ok,
first lets sort out your link. You dont need the javascript: at the begingin of the onclick event. You also dont need to dynamically write the img tag. Just assign it and ID and then javascript can manipulate it from there
Code:
<a href="#" onClick="hide('menu1')"><img src="images/plus.jpg" id="menu1_icon" border="0"></a>
Ok, now we need to add the command to your function to change the image to either plus or minus.
Code:
<script type="text/javascript">
<!--
var counter=0;
var plusminus
function hide(el)
{
	if(counter==0)
	{
		document.getElementById(el).style.display="block";
		counter=1;
		[b]document.getElementById(el + "_icon").src = "images/minus.jpg";[/b]
	}
	else
	{
		document.getElementById(el).style.display="none";
		counter=0;
		[b]document.getElementById(el + "_icon").src = "images/plus.jpg";[/b]
	}
}
//-->
</script>

Hope that helps

Westbury

If it aint broke, redesign it!
 
cool, thanks Westbury!! that totally helped me out, it works now :) and i understand now why my code wasn't working...thanks a lot!!!

karren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top