How does jQuery select HTML input with its name and atttribute?

I have a very common question: I want to get the value of HTML input using a jQuery selector with its name and a specific attribute, for example, marked. Here 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 he did not return the correct value. Hope someone helps me. Thanks.

+4
source share
3 answers

You need to enter an element and then an attribute, for example $('element[attribute="value"]')

 $('input[name="gender"]:checked').val(); 
+6
source

With the :checked selector you don't need to specify a value, try the following:

 var gender = $('input[name="gender"]:checked').val(); 

Additional information in API documents

+4
source
 $('input[name="gender"]:checked').val(); 

Are you looking for something like that?

+2
source

All Articles