Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 9th, 2005, 07:05 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old March 10th, 2005, 08:25 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old March 10th, 2005, 11:09 AM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey thanks. It works like a charm.

Kyle
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.