I am putting together a form and some content will be conditional based on a user's radio button selections. I've got code that works as I intend (hide or show followup content based on selection). However, I can only do it using onclick within the html (i.e. "onclick=checkRadioAnswer()"). See snippets below for example.
I would prefer to keep it all in one JS file and not use onclick in the HTML. I figured addEventListener() would be the way to go but I cannot get it to work for the life of me.
Any suggestions on how I might rework this code so that it will act the same as my current setup but without using onclick in the html? I also need to keep this plain JS. Thanks
window.onload = function setDefaultState() {
checkRadioAnswer();
};
const changingContent = document.getElementById("changing-content");
function checkRadioAnswer() {
var radios = document.getElementsByName("genericRadio")
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
if (radios[i].value == "1") {
changingContent.style.display = "none";
}
if (radios[i].value == "2") {
changingContent.style.display = "block";
}
if (radios[i].value == "3") {
changingContent.style.display = "none";
}
if (radios[i].value == "4") {
changingContent.style.display = "block";
}
}
}
};
<fieldset class="fieldset-radio">
<legend>
<h1 class="question-primary">The question goes here</h1>
</legend>
<div class="field-radioButton">
<label for="answer1">
<input id="answer1"
type="radio"
name="genericRadio"
value="1"
checked="checked"
onclick="checkRadioAnswer()">
<span>1st answer text</span>
</label>
</div>
<div class="field-radioButton">
<label for="answer2">
<input id="answer2"
type="radio"
name="genericRadio"
value="2"
onclick="checkRadioAnswer()">
<span>2nd answer text</span>
</label>
</div>
<div class="field-radioButton">
<label for="answer3">
<input id="answer3"
type="radio"
name="genericRadio"
value="3"
onclick="checkRadioAnswer()">
<span>3rd answer text</span>
</label>
</div>
<div class="field-radioButton">
<label for="answer4">
<input id="answer4"
type="radio"
name="genericRadio"
value="4"
onclick="checkRadioAnswer()">
<span>4th answer text</span>
</label>
</div>
</fieldset>
<div id="changing-content">
<p>Content depending on answer goes here</p>
</div>