Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 402
Brady Zhu Avatar asked Jan 22 '13 16:01

Brady Zhu


3 Answers

You need to type the element then the attribute, like this $('element[attribute="value"]')

$('input[name="gender"]:checked').val();
like image 87
Anton Avatar answered Sep 21 '22 16:09

Anton


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

like image 21
Rory McCrossan Avatar answered Sep 20 '22 16:09

Rory McCrossan


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

Are you looking for something like that?

like image 45
Andrea Turri Avatar answered Sep 23 '22 16:09

Andrea Turri