1
I need to take the value of one radio
and use it to call an image, follows example below:
function raceimg(elemento){
document.getElementsById("raceimgchange").src = elemento.value;
};
<html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Builder</title>
<script type="text/javascript" src="JS/racetype.js"></script>
</head>
<body>
<section id="racetype">
<p><input type="radio" name="race" onclick="raceimg(this)" value="img/doly.jpg"> Human </p>
<p><input type="radio" name="race" onclick="raceimg(this)" value="img/shrek.jpg"> Dwarf </p>
<p><input type="radio" name="race" onclick="raceimg(this)" value="img/pikachu.jpg"> Elf </p>
</section>
<section id="raceimg">
<img src="img/doly.jpg" alt="Doly" id="raceimgchange">
</section>
</body>
</html>
I want to use the value
of radio
's to designate image Urls so I don’t have to declare them all in Javascript. How do I make it work?
Alternative solution I found below, but the images are broken, but the exchange is being made:
function raceimg(elemento){
document.getElementById("texto1").innerHTML = elemento;
document.getElementById("img1").innerHTML = "<img src='"+elemento+".jpg' alt='img/"+elemento+".jpg'>";
};
<html><!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Builder</title>
<script type="text/javascript" src="JS/racetype.js"></script>
</head>
<body>
<section id="racetype">
<p><input type="radio" name="race" onclick="raceimg('doly')"> Human </p>
<p><input type="radio" name="race" onclick="raceimg('shrek')"> Dwarf </p>
<p><input type="radio" name="race" onclick="raceimg('pikachu')"> Elf </p>
<img id="raceimgchange">
</section>
<section id="raceimgagem">
<span id="texto1"></span> ---> <span id="img1"></span>
</section>
</body>
</html>
Thank you but could you insert this your solution along with the for? I couldn’t see how he would get the value of the radios depending on the click of the way you did there.
– Tandy
Do not understand, you do not want that when selecting a radiobutton it changes the value of the image with the value of the right radiobutton?
– Gustavo Luciano
I want the image to change according to the radio I chose. when I click on a radio, the image changes according to the url that is in the value of the radio I clicked on.
– Tandy
The code I sent just above does just that, I modified the code by adding the <img tag>.
– Gustavo Luciano
I put my full HTML up there for you to see, what I have to change in it?
– Tandy
It’s all right in html, just change your javascript function to my function.
– Gustavo Luciano
Okay, I changed the code as you indicated, there’s html and JS. and yet it’s not changing the image when I change the radio.
– Tandy
Got it! Look how it got in the second part of the question. It worked here, vlw by the help!
– Tandy
The problem with the top code is that you are using getElementsById and the correct is getElementById
– Gustavo Luciano