JQuery - find a radio button by value

How would I use JQuery to find a radio button by its value?


Solution 1:

Try this:

$(":radio[value=foobar]")

This will select all radio buttons with the attribute value="foobar".

Solution 2:

I'm using jQuery 1.6 and this did not work for me: $(":radio[value=foobar]").attr('checked',true);

Instead, I'm using: $('[value="foobar"]').attr('checked',true); and it works great.

The actual code I'm using clears the radios first and uses prop instead of attr

$('[name=menuRadio]').prop('checked',false);

$('[name=menuRadio][value="Bar Menu"]').prop('checked',true);

Solution 3:

This can be achieved by this too:

$('input[type="radio"][value="aaa"]').val();

This will select the radio which has the value of aaa


With .filter() method:

var radio =  $('input[type="radio"]').filter(function(){
                       return this.value === 'aaa';
                      }).val();