Radio Button response

i’m trying to get the value of a radio button in an alert on the button has been checked. I have tried grabbing one and checking is it has ben checked, but that doesn’t work the way I’ve done it. Can anyone help?

<div class="modal modal1 show" id="modal1">
        <p class="hed">
          Question #1
          <button class="close" id="close">X</button>
        <p class="bod">
          Who created the Internet?
        <form style="text-align: left;">
          <!-- <label for="q1a1">Precise date unknown</label> -->
          &emsp;<input type="radio" name="q1" value="Wrong!"> Bob khan<br>
          <!-- <label for="q1a2">October 5th, 1991</label> -->
          &emsp;<input type="radio" name="q1" value="Wrong!"> Vint Cerf<br>
          <!-- <label for="q1a3">1991-e</label> -->
          &emsp;<input type="radio" name="q1" value="Correct!"> Both above

Thanks ahead of time

@dportilla0001 you think you can add your full code in codepen or another platform so other developers can actually see your full structure, and so everyone can help.

  1. Create an event listener that listens for a click in the submit button, by grabbing its element.
  2. Start by looping the name tag elements in this case name="q1" with an old classic for loop.
  3. Add a condition if the current element is checked, then display the current element with its value in the alert window.

Let us know if you have any questions.


@imendieta ,

Here is codepen. I can sadly not allow you to edit, so look over it and tell me if I should put it on a pen.


Did you find out the solution @dportilla0001 ? have you have a working example code that you can provide. ?


I found my problem but I’m having it again, so I’m looking for my earlier error.

Here is an easy way of getting what you want I saw your code and this can be an easy way too, for what you wanted to accomplish. Just giving you a code structure of what you want to accomplish and that you were asking, Giving you another alternative of writing the code. :slight_smile:

document.getElementById('submit').onclick = function() {
    var selected = document.querySelector('input[type=radio][name=q1]:checked');

Hope this helps. :slight_smile:

Thank you, @imendieta ,
Sorry I did not mark this solution before wards, I have not been on my freeCodeCampForum for a month. I have moved one from my troubles with the radio buttons thought I see that my code ins’t to far from yours. Thank you for your patience.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.