How to change a radio button value [duplicate]
Asked Answered
S

3

5

Possible Duplicate:
how to check a radio button with jQuery?

How to change a radio button value.

jQuery('input:radio[name=search][id=search-damages]').checked = true;

i have tried this but it is not working

<input type="radio" name="search" id="search-vehicle" value="search-vehicle" checked>
<input type="radio" name="search" id="search-damages" value="search-damages">
Semifinalist answered 28/7, 2012 at 10:36 Comment(0)
E
11

How to change a radio button value?

checked property doesn't change the value of radio button. note that checked property is one the DOM INPUT Element properties and not one of the jQuery object properties(by selecting an element using jQuery, a jQuery object is created). If you want to change this property you should first convert the jQuery object into a DOM Element object.

$('#search-damages')[0].checked = true;

or use the jQuery object's prop() method:

$('#search-damages').prop('checked', true)

If you want to change the value of an input you can use the jQuery val() method:

$('#search-damages').val('new value');
Elongate answered 28/7, 2012 at 11:15 Comment(1)
I wish more input-related jQuery questions would more accurately specify to use prop instead of attr, thanks.Evetteevey
B
1

I'm a fan of "use jQuery when reduce complexity, use JS in other cases", so I would go for:

$("#search-damages")[0].checked = true;

Otherwise, if you prefer use jQuery syntanx, you could use:

$("#search-damages").prop("checked", true);

See: http://api.jquery.com/prop/

Brook answered 28/7, 2012 at 10:42 Comment(0)
S
0

Try this:

$('#search-damages').attr('checked','checked');
Secondly answered 28/7, 2012 at 10:38 Comment(1)
Its working you can have a look at the live site: jsfiddle.net/MAhmadZ/S4EGXSecondly

© 2022 - 2024 — McMap. All rights reserved.