views:

51

answers:

2

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>

This is killing me. Thanks in advance.

Edit: Updated the example to show how I'm setting the UI.

+2  A: 
$('[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
Yanick Rochon
I'm thoroughly sorry. I just didn't think there'd be much to such details, given how a regular radio button is pretty straight forward. oO./
Rafael Belliard
+3  A: 

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.

Nick Craver
the buttonset part of the question wasn't there when I answered. :/
Yanick Rochon
I would've never thought of this. Thanks.
Rafael Belliard
@Yanick - It was...he mentioned jQuery UI from the start, that's why my comment posted even before your answer inquires about it :)
Nick Craver
@Nick Crave, I beg to differ. When I answered the question, there was only the HTML part as code (plus the .val() call, that's it). There was no mention of a bottonset. Though I might have expected it, I didn't think the OP was using JQuery UI.
Yanick Rochon
The original question title has jQuery UI in it, you can see the revisions by clicking the edit link on the question which goes here: http://stackoverflow.com/posts/3569794/revisions (why do you think I asked about it 2 minutes after the original answer?) :)
Nick Craver