rebelliard
rebelliard

Reputation: 9611

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>

Upvotes: 11

Views: 14593

Answers (4)

daudichya
daudichya

Reputation: 379

i usually do a:

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

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

Upvotes: 2

Thomas Heymann
Thomas Heymann

Reputation: 265

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

Upvotes: 8

Nick Craver
Nick Craver

Reputation: 630637

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.

Upvotes: 20

Yanick Rochon
Yanick Rochon

Reputation: 53626

$('[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

Upvotes: 4

Related Questions