How to find out which radio button is selected using jQuery?
Hello everyone! I'm a beginner in web development and trying to understand radio buttons in a form. I have a group of radio buttons for selecting the user's gender:
```
Male
Female
```
I want that when the button is clicked, the value of the selected radio button (e.g., "male" or "female") is output to the console. I tried writing this code:
```
$('#submit').click(function() {
var selected = $('input[name="gender"]:checked').val();
console.log(selected);
});
```
But sometimes it works, and sometimes it outputs `undefined`. I don't understand why. Maybe I'm using the `:checked` selector incorrectly? Or is the problem that the radio buttons are not wrapped in a <
No answers yet.
Log in to leave an answer