That's great! Thanks folks for all your help.
I've learned a lot from this topic.
The code I'm writing is to reset a rollover button on mouse-out, but rather than write a separate line of code for each button, I wanted to parameterize it based on the id of the button passed to the function.
I've now successfully achieved this with your help.
Code follows:
Code:
<head>
<script language="javascript">
<!--
var sections = new Array("News", "Discography", "Media", "Projects", "Biography", "Feedback", "Thanks", "Elsewhere", "Live", "Shop");
var max_buttons = "10"
function button_hover_off(section){
area=sections[section]
for (var num_button=0; num_button<max_buttons; num_button++){
if(sections[num_button]==area){
var selected_button="But_" + area
document.getElementById(selected_button).src = "nav_graphics\\Up_" + area + ".png";
}
}
}
//-->
</script>
</head>
<body>
<!------------ Button #1 ---------->
<a href=#
onmouseover="button_hover_on(0)"
onmouseout="button_hover_off(0)"
onclick="button_click_on(0)">
<img id="But_News" border="0" src="nav_graphics\\Up_News.png">
</a>
<br>
<!------------ Button #2 ---------->
<a href=#
onmouseover="button_hover_on(1)"
onmouseout="button_hover_off(1)"
onclick="button_click_on(1)">
<img id="But_Discography" border="0" src="nav_graphics\\Up_Discography.png">
</a>
<br>
....
</body>
Next thing I'm going to try is to write a for loop in the body of the page to display the buttons initially using document.write. This'll mean that I can write all ten buttons in about four lines of code therefore shrinking my code even further.
I think it'll look something like this, but my code is still buggy:
Code:
<body>
<script language="JavaScript">
<!--
for (var loop=0; loop<max_buttons; loop++){
document.writeln("<a href=# onmouseover=\"button_hover_on(" + loop + ")\" onmouseout=\"button_hover_off(" + loop + ")\" onclick=\"button_click_on(" + loop + ")\">")
document.writeln("<img id=\"But_" + sections[0] + " border=\"0\" src=\"nav_graphics\\Up_" + sections[loop] +".png\">")
document.writeln("</a><br>")
}
//-->
</script>
</body>
Thanks again for your help,
Andy
Bookmarks