- Is there a way to save the value of my
radio button
in atext input
after I press the button? I have tried but the moment I press the button itinput text
disappears and the plain text appears. - Is there a way to automatically display
input text
the value ofradio button
the when pressed?
Any answer of the two questions would help me a lot.
This is the code I'm using, thanks in advance.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript - Obtain the value of a radio button</title>
<script>
function capturar() {
var resultado = "ninguno";
var porNombre = document.getElementsByName("deacuerdo");
// Recorremos todos los valores del radio button para encontrar el
// seleccionado
for (var i = 0; i < porNombre.length; i++) {
if (porNombre[i].checked)
resultado = porNombre[i].value;
}
document.getElementById("resultado").innerHTML = " \
Por Nombre: "+ resultado;
}
</script>
</head>
<body>
<h1>Obtain the value of a radio button</h1>
<form id="form1">
<p>Deacuerdo: Si<input type="radio" name="deacuerdo" value="si"> No<input type="radio" name="deacuerdo"
value="no"></p>
</form>
<input type="button" value="What is the value of the radio button" onclick="capturar()">
<div id="resultado"><input type="text"></div>
</body>
</html>
The error is that you are overwriting what is in the div with id
resultado
for this reason its input disappears, you would have to addid
type to your inputtext
so that inside it you add the value through.value
. I add comments in the code for your better understanding. :)