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?
Code:
HTML
@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.
Create an event listener that listens for a click in the submit button, by grabbing its element.
Start by looping the name tag elements in this case name="q1" with an old classic for loop.
Add a condition if the current element is checked, then display the current element with its value in the alert window.
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.
document.getElementById('submit').onclick = function() {
var selected = document.querySelector('input[type=radio][name=q1]:checked');
alert(selected.value);
}
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.