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>
i usually do a:
$('[name="radio"][value="false"]').prop("checked", true).trigger("change");
Please use "prop" instead of "attr" to change the "checked" state.
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.
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
$('[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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With