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?
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>