Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!