Tech in a Galagzee, Not So Far Away.
Selecting radio button in JavaScript with jQuery
I’m learning jQuery, and really like all the endless additional features (not to mention the ease of writing code!) it brings to JavaScript. But for a newbie some things take time to sink in, especially since the concept of jQuery is a bit different from vanilla JavaScript.
It took me couple of hours to figure out the following two lines to control default selection of radio buttons in a form (I knew how it’s done in plain JavaScript, of course, but it was the jQuery format that was something new to figure out). Setting the radio button attribute isn’t enough; the value needs to be set separately. I might write a simple helper plugin for jQuery later once I become more familiar with it so that setting the attribute automatically sets the corresponding value to the radio group.
$("input[@name='us_edition']").val("both");
$("#us_edition_both").attr("checked", "checked");
That piece of code selects the "us_edition_both" (with value "both") radio button in my form:
<input name="us_edition" type="radio" id="us_edition_both" value="both" \
onClick="show_us_mailing();" checked="checked">
<input name="us_edition" type="radio" id="us_edition_digi" value="digi" \
onClick="hide_us_mailing();">
Of course it shouldn’t have been this difficult to figure out and, of course, it isn’t. A month from now I’ll snicker at myself having spent more than two hours figuring out how to do this.
| Print article | This entry was posted by Ville Walveranta on 21 September 2007 at 02:52, and is filed under Technical. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
And to get the selected value:
var selected = $(“input[@name='us_edition']:checked”).val();
By the way: I doubt one needs to set the value; I think
$(“#us_edition_both”).attr(“checked”, “checked”);
is sufficient to make the browser do the rest.
about 1 year ago
I tried this out but couldn’t get it working. I ended up using the form plugin from jquery –> http://malsup.com/jquery/form/#download
The plugin works a treat
about 1 year ago
And I was looking and looking and looking, and finally found how to set a radiobutton to checked with jQuery on your blog. Thanks for sharing, cheers
about 1 year ago
That was what i was looking for !!
about 1 year ago
This is exactly what I’m looking for! Thanks for sharing.
about 1 year ago
Thanks. That very helpfull.
.
As for Arjan tip – not work at FireFox. Not sure about other web browsers
about 1 year ago
Arjan is using a deprecated way to point at an object via its name. I did the same mistake, the correct code is:
var selected = $(”input[name='us_edition']:checked”).val();
without the @
wasted like three hours figuring that out…
about 1 year ago
Marcello, thanks for pointing that out!
about 1 year ago
The @ is removed for the latest version of jQuery. Listed in the release notes, Marcello.
about 1 year ago
Nice, was looking for “jquery setting radio button checked” in google, yours was one of the first that came up. Thanks!
about 9 months ago
To set a button, you may use the following method:
$(‘input[name="us_edition"]‘).each(function() {
if($(this).val() == “digi”) {
$(this).attr(“checked”,”checked”);
}
});
This will set the radio button to be checked for the “digi” value.
about 6 months ago
Hi,
Thanks for this, I always forget how to do it and you got me out of a really tight squeeze!
about 4 months ago
Reply to Martyn. To use on a button and a group of radios, use: $(‘input[name=X_Y_Z][value=A_B_C]‘).attr(‘checked’,'checked’)
about 3 months ago
Or you could just use
$(this).click();
about 3 weeks ago
@Rio : I didn’t think about this one. It’s what I needed, thanks.
about 1 week ago
Thanks for this! A Friend of mine was having trouble with this and I was able to use this post to teach myself something new, and pass it on to another programmer.
Thanks again!