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

145
Views
The radio inputs from my html are not interacting well with my if statements in javascript

I'm a beginner and am trying to make the radio button submission generate the corresponding result below, fairly simple idea. Here's where I'm stuck

<h3>Choose Your Variable</h3>
<form action="#">
    <label for="itemtype"><input id="xmon" type="radio" name="monname" value="x">Xmon</label>
    <label for="itemtype"><input id="ymon" type="radio" name="monname" value="y">Ymon</label>
    <label for="itemtype"><input id="zmon" type="radio" name="monname" value="z">Zmon</label><br>
    <button type="submit">Submit</button> <button type="reset">Reset</button>
</form>

<p id="item">Your item is...</p>

<script>
    let answer = document.querySelectorAll("[name='monname']"); 
    let form = document.querySelector("form");

    form.addEventListener("submit", redirect);

function redirect() {
    event.preventDefault();
    if(answer.value == "x"){
        document.getElementById("item").innerHTML="Amon";
    }else if(answer.value == "y"){
        document.getElementById("item").innerHTML="Bmon";
    }else {
        document.getElementById("item").innerHTML="Cmon";
    }
}
</script>

On let answer = document.querySelectorAll("[name='monname']"); the final result is always "Cmon" no matter the radio button selected.
If I change it to let answer = document.querySelector("[name='monname']"); the result is always "Amon" no matter the radio button selected.

I have no idea what I'm missing. General code feedback is also appreciated.

about 4 years ago · Juan Pablo Isaza
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!