Forum Moderators: open

Message Too Old, No Replies

toggling an image with a radio button

         

gleddy

12:52 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



heya,

I need to use a radio button to toggle an image. so when the radio button is checked, then the "on" image appears, otherwise the 'off' image appears.

has anyone done this before?

cheers
s.

Bernard Marx

10:47 pm on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one way of doing it.

<HTML><HEAD>
<script>

var radioImages =
{
on :createImage("on.gif"),
off:createImage("off.gif")
}

function createImage(src){ var img = new Image(); img.src = src; return img; }

function radioToggle(button)
{

var form = button.form,
rads = form.elements['rad'],
rad, k = 0,
images = form.getElementsByTagName("img");

while(rad=rads[k])
images[k++].src = rad.checked
? radioImages.on.src
: radioImages.off.src ;
}

</script>
</head>

<form>
<input type="radio" name="rad" onclick="radioToggle(this)">
<img src="off.gif" width="30" height="30"><br>
<input type="radio" name="rad" onclick="radioToggle(this)">
<img src="off.gif" width="30" height="30"><br>
<input type="radio" name="rad" onclick="radioToggle(this)">
<img src="off.gif" width="30" height="30"><br>
<input type="radio" name="rad" onclick="radioToggle(this)">
<img src="off.gif" width="30" height="30"><br>
<form>
</BODY></HTML>

gleddy

2:31 pm on Aug 20, 2004 (gmt 0)

10+ Year Member



thanks mate... works exactly how I want it to...

s.