Dave Dave - 4 months ago 16
Javascript Question

Get the currently selected radio button in a jquery ui buttonset without binding to click

I have radio buttons like this:

<div id="radio">
<input type="radio" id="radio1" name="radioOption" /><label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radioOption" checked="checked" /><label for="radio2">Choice 2</label>
<input type="radio" id="radio3" name="radioOption" /><label for="radio3">Choice 3</label>
</div>


And apply the button set like this:

$(function () {
$("#radio").buttonset();
});


I need to get the selected radio button without binding to the click event.

Answer

Damn just realised my selecter had a typo, but if it helps anyone else:

If you dont want to use the click event, which I didn't you could do the following: If you're looking for the element it self use:

$("#radio :radio:checked");

If you're looking for the id of the element use:

$("#radio :radio:checked").attr('id');

If you're need to know what the text of the value selected then this should work:

$("#radio :radio:checked + label").text();