5

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.

Brady Zhu
  • 1,305
  • 5
  • 21
  • 43
  • poss dupe:[http://stackoverflow.com/questions/4813219/jquery-checkbox-value](http://stackoverflow.com/questions/4813219/jquery-checkbox-value) – markpsmith Jan 22 '13 at 16:05

3 Answers3

6

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

$('input[name="gender"]:checked').val();
Anton
  • 32,245
  • 5
  • 44
  • 54
4

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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2
$('input[name="gender"]:checked').val();

Are you looking for something like that?

Andrea Turri
  • 6,480
  • 7
  • 37
  • 63