Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2005, 07:05 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default using onClick to change images

I am trying to figure out how to change an image according to the value of a radio button. I have a basic script that accomplishes this if it stands alone. The problem is there are 4 groups of radio buttons on the page and I want each group to control its own picture. The script I have just hunts out the first image in the html code and changes it. I know that somehow I need to pass a location or some indicator into the function for it to determine which image to change. Any suggestions would be greatly appreciated. Below is the code I'm using now.


<html>

<head>
<script>
function display(pic)
{
       document.images[0].src=pic;
}
</script>
</head>

<body>
<form name="a">
<br><br>
<input type="radio" name="a" value="0" onClick=display('images/arrow1.bmp')>
yes<br>
<input type="radio" name="b" value="1" onClick=display('images/arrow2.bmp')>
no<br>
<input type="radio" name="c" value="2" onClick=display('images/arrow1.bmp')>
other
<img border="0" src="images/arrow1.bmp"><br>
<img src="images/default.bmp"><br>
</body>

</html>

TIA
Kyle
 
Old March 10th, 2005, 08:25 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to add a second parameter to your function, the index of the image to alter:
Code:
onClick=display('images/arrow1.bmp', 0)

function display(pic, index)
{
       document.images[index].src=pic;
}
Each group of radio buttons controls its own indexed image, so your original group all have a second parameter of 0, the next block have a second parameter of 1 etc.

--

Joe (Microsoft MVP - XML)
 
Old March 10th, 2005, 11:09 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey thanks. It works like a charm.

Kyle





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
Change onclick of a button me_zeta HTML Code Clinic 1 November 20th, 2006 10:34 AM
change <td> value onClick crmpicco Javascript How-To 2 August 23rd, 2005 07:31 AM
onClick crmpicco Javascript How-To 1 March 21st, 2005 09:18 AM
onClick change bgcolor of entire row Pallav Javascript How-To 2 October 21st, 2004 12:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.