Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQueryUI Radio/Check buttons not changing programmatically

I can predicably read the value of a jQueryUI radio button, however, I am not able to set it programmatically. No matter which methods I use to change the radio button's value, jQueryUI interface will not update its interface.

<div id="radio">
    <input type="radio" name="radio" value="true" checked="checked" />Yes
    <input type="radio" name="radio" value="false" />No
</div>

<script type="text/javascript">
    $('#radio').buttonset();

    // One of the many ways that won't work.
    $('[name="radio"]:radio:checked').val("false");
</script>
like image 917
rebelliard Avatar asked Aug 25 '10 20:08

rebelliard


4 Answers

i usually do a:

$('[name="radio"][value="false"]').prop("checked", true).trigger("change");

Please use "prop" instead of "attr" to change the "checked" state.

like image 114
daudichya Avatar answered Nov 08 '22 06:11

daudichya


Once you update the value, like this:

$('[name="radio"][value="false"]').attr("checked", true);

You need to tell jQuery UI to update, like this:

$('#radio').buttonset("refresh");

You can give it a try here.

like image 25
Nick Craver Avatar answered Nov 08 '22 07:11

Nick Craver


i usually do a:

$('[name="radio"][value="false"]').attr("checked", true).trigger("change");

the buttonset("refresh") call is bound to the change event which is simply not triggered when you're changing the value programatically - triggering it manually solves the ui issue and you don't need to worry about where the buttonset is

like image 33
Thomas Heymann Avatar answered Nov 08 '22 07:11

Thomas Heymann


$('[name="state"]:radio:checked').attr('checked', true);   // checked
$('[name="state"]:radio:checked').removeAttr('checked');   // unchecked

** NOTE **

$(':radio').val();  // same as $(':radio').attr('value');

Thus :

$(':radio[checked]').val(); // -> the value of the first checked radio button found
like image 4
Yanick Rochon Avatar answered Nov 08 '22 07:11

Yanick Rochon