1

I have a 5-question form, where the questions are shown one at a time to the user, like a quiz. Question 1 uses input type="radio". I would like to know if there is a way not to have to call the function checkObjective() for each input, and thus avoid repetitions.

My current code is working and this is it:

<form class="questions_box formulario" action="enviar-landing-page-v3.php" method="post">
    <div id="question-1">
        <h3>The question is: ... ?</h3>
        <input type="radio" name="objetivo" id="question-1-answer-a" value="1" required oninput="checkObjective()" /> Answer 1.<br />
        <input type="radio" name="objetivo" id="question-1-answer-b" value="2" oninput="checkObjective()" /> Answer 2.<br />
        <input type="radio" name="objetivo" id="question-1-answer-c" value="3" oninput="checkObjective()" /> Answer 3.<br />

        <div class="text-end mt-3">
            <input type="submit" id="submit1" class="btn btn-primary btn-lg" value="Enviar Resposta" style="display: none;" />
        </div>

        <script type="text/javascript">
            function checkObjective() {
                if (document.getElementsByName("objetivo").value == "") {
                    document.getElementById("submit1").style.display = "none";
                } else {
                    document.getElementById("submit1").style.display = "block";
                }
            }
        </script>
    </div>
</form>
Jonas
  • 121,568
  • 97
  • 310
  • 388

2 Answers2

2

If I understood your question properly, you don't want to add oninput="checkObjective()" repeatedly on each individual radio button.

In that case, you can simply give them a common class and attach an eventListener to them.

<input type="radio" class="radionButton" name="objetivo" id="question-1-answer-a" value="1" required /> Answer 1.<br />
<input type="radio" class="radionButton" name="objetivo" id="question-1-answer-b" value="2" /> Answer 2.<br />
<input type="radio" class="radionButton" name="objetivo" id="question-1-answer-c" value="3" /> Answer 3.<br />
document.querySelectorAll('.radioButton').forEach(item => {
  item.addEventListener('click', event => {
    checkObjective();
  });
});
Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
I_love_vegetables
  • 1,575
  • 5
  • 12
  • 26
0

const radios = document.querySelectorAll('div[id^="question"] input[type="radio"]');
for (const radio of radios) {
  radio.addEventListener('input', checkObjective);
}

function checkObjective() {
  const n = Number(this.id.match(/question-(\d+)/)[1]);
  document.getElementById(`submit${n}`).style.display = 'block';
  const next = document.getElementById(`question-${n + 1}`);
  if (next) { next.style.display = 'block'; }
}
div[id^="question"]:nth-of-type(n+2) {
  display: none;
}
<div id="question-1">
                    <h3>The question is: ... ?</h3>
                    <input type="radio" name="objetivo" id="question-1-answer-a" value=1 required> Answer 1.<br>
                    <input type="radio" name="objetivo" id="question-1-answer-b" value=2> Answer 2.<br>
                    <input type="radio" name="objetivo" id="question-1-answer-c" value=3> Answer 3.<br>

                    <div class="text-end mt-3">
                        <input type="submit" id="submit1" class="btn btn-primary btn-lg" value="Enviar Resposta" style="display: none;" />
                    </div>
                </div>

                <div id="question-2">
                    <h3>The question is: ... ? (2)</h3>
                    <input type="radio" name="objetivo2" id="question-2-answer-a" value=1 required> Answer 1.<br>
                    <input type="radio" name="objetivo2" id="question-2-answer-b" value=2> Answer 2.<br>
                    <input type="radio" name="objetivo2" id="question-2-answer-c" value=3> Answer 3.<br>

                    <div class="text-end mt-3">
                        <input type="submit" id="submit2" class="btn btn-primary btn-lg" value="Enviar Resposta2" style="display: none;" />
                    </div>
                </div>
idfurw
  • 5,727
  • 2
  • 5
  • 18