Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
How to dynamicly set value radiobutton with JS in HTML

I'm a novice in JS and trying to put some text dynamicly as a optionvalue for the radio button. But I can't seem to make the text appear. What am I doing wrong?

HTML

<form id="formulier">
<label for="a">
<input type="radio" id="a" name="a">
<!--here I want to show a value from an array ic answer[0]-->
<!--tried also this:   <script>document.write(answer[0]);</script>    -->
</label>
</form>

JS

var answer = <?php echo json_encode($antwoorden); ?>;
const a = document.getElementById("a");

// Ive tried:
a.value = answer[0];
a.innerHTML = answer[0];
a.innerText = answer[0];

None of them shows up. How can I make the text show up in HTML?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't output text by setting it as a value of radio buttons.

Instead you could set it as the textual content of the related label element.

To be able to select the associated label easily with JavaScript, assign the same class as the ìd of the <input type="radio"> element to it.

HTMl & JS Snippet

var answer = 'Dynamically inserted Message';
const a = document.getElementsByClassName("a")[0];
a.innerHTML = answer;
<form id="formulier">
  <input type="radio" id="a" name="a">
  <label for="a" class="a"></label>
</form>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda