I got a very common question that I want to get the value of a HTML input by jQuery selector with its name and specific attribute like checked. Following is my case:
<input type="radio" name="gender" value="man" checked="checked" />
<input type="radio" name="gender" value="women"/>
I tried the following code:
var gener = $("name='gender':checked=checked").val();
But it didn't return a correct value. Hope somebody gives me help on it. Thanks.
You need to type the element then the attribute, like this $('element[attribute="value"]')
$('input[name="gender"]:checked').val();
With the :checked
selector, you don't need to provide a value, try this:
var gender = $('input[name="gender"]:checked').val();
More information in the API docs
$('input[name="gender"]:checked').val();
Are you looking for something like that?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With