Forum Moderators: open

Message Too Old, No Replies

toggle images

onclick switch between two images

         

andre73

3:43 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



can anyone help me make a script that switches 1 image for another?

example: I have an arrow indicating a menu item has submenus, it called downarrow.gif. When I click this arrow I want to exchange it for uparrow.gif. When I click the upparrow.gif I want to switch back to downarrow.gif again. is this possible?
Any help is appreciated.

isitreal

6:50 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have to put in the correct path here to your images of course.

<html>
<head>
<script type="text/javascript">
/*swaps image by parameters, only activates if images preloaded already
1 is down, 0 is up, page defaults to up, switch to suite your preferences
the a_menu_items array is global, so it keeps the menu items value as set, the value will change
with each click.
Each menu item image must have the 'name="menu3"' or whatever, you pass the number
to the changeimage function, it uses that to write out which name to use.
This method is more stably supported cross browser, it will always work on all
browsers out there, unlike getElementById.
the a_menu_items array should have as many menu items as you have, although it doesn't really matter as long
as it has the same or more values as you have menu items
*/
// the number of array items must be at least
// equal to your number of menu items, these are the default start values that will change on each click
var a_menu_items = new Array(1, 1, 1, 1);

function changeimage(number)
{
if (document.images && g_bPreloadFlag)
{
imgName = 'menu' + number;
// loop through the menu items array
for ( i = 1; i < (a_menu_items.length + 1); i++ )
{
if ( number == i )
{
//pick the opposite state currently in the value
state = ( a_menu_items[i] == 0 )? 1 : 0;
// after assigning, switch the value to its opposite
( a_menu_items[i] == 0 )? a_menu_items[i] = 1 : a_menu_items[i] = 0;
break;
}
}
document.images[imgName].src = g_aMenu[0][state].src;
}
}

var g_bPreloadFlag = false;
var g_aMenu = new Array();

function preloadimages()
{
if (document.images)
{
var aOffOn = new Array(1);
g_aMenu[0] = aOffOn;
g_aMenu[0][0] = new Image();
g_aMenu[0][0].src = 'menu_up.gif';
g_aMenu[0][1] = new Image();
g_aMenu[0][1].src = 'menu_down.gif';
g_bPreloadFlag = true;
}
}
</script>
</head>

<body onload="preloadimages()">

<a href="#" onclick="changeimage(1)">
<img src="menu_down.gif" name="menu1">menu item</a><br>
<a href="#" onclick="changeimage(2)">
<img src="menu_down.gif" name="menu2">menu item</a><br>
</body>

</html>

andre73

9:36 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Wow, that's great! Thanks!
:-)

isitreal

12:31 am on Feb 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks, your up down menu arrow was such a good idea I couldn't resist working it out, I think I'll use something like that too for expanding menus, plus I was working on an image rollover version of that script this weekend for a client, so it was still fresh in my head.