Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change image on click of radio button in html using javascript,but it doesnt seem to change.I have tried with following code but its not working.Please suggest if there is something wrong in code and advice me to correct it.
here's the code:
C#
<html>
<head>
<title>sample</title>
<script type="text/javascript">

JavaScript
function ChangeImage(newimage)
{
document.rd.src=newimage;
}
</script>
</head>
<body>
<h1>image</h1><br>
<img src="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg"  name="rd" width="100px" height="100px">

<input type=radio  value="a" name="rd1"  önclick="ChangeImage('C:\Documents and Settings\All Users\Documents\My Pictures\Sample 

Pictures\Winter.jpg')">Winter	
<input type=radio value="b" name="rd1"  önclick="ChangeImage('C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue 

hills.jpg')">Blue hills	
<input type=radio value="c" name="rd1"  önclick="ChangeImage('C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water 

lilies.jpg')">Water lilies

<div id="'imagedest">
</div>
</body>
</html></br>
Posted
Updated 17-Feb-14 20:22pm
v3

1 solution

Try like this :
XML
<script language=javascript type=text/javascript>
  function check_value(){
    switch(document.test.field.value){
       case "one":
         document.getElementById("imagetest").innerHTML = "<img src='images/firstimage.png'>";
         break;
       case "two":
          document.getElementById("imagetest").innerHTML = "<img src='images/secondimage.png'>";
          break;
       case "three":
          document.getElementById("imagetest").innerHTML = "<img src='images/thirdimage.png'>";
          break;
    }
  }
</script>


<form name='bob'>
    <input type="radio" name="field" value="one" onchange='check_value()'>one
    <input type="radio" name="field" value="two" onchange='check_value()'>two
    <input type="radio" name="field" value="three" onchange='check_value()'>three
</form>

<div id='imagetest'>
</div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900