How to select a radio button by default? [duplicate]
Asked Answered
R

5

902

I have some radio buttons and I want one of them to be set as selected by default when the page is loaded. How can I do that?

<input type="radio" name="imgsel"  value=""  /> 
Recollect answered 8/4, 2011 at 8:18 Comment(0)
M
1427

XHTML solution:

<input type="radio" name="imgsel" value="" checked="checked" />

Please note, that the actual value of checked attribute does not actually matter; it's just a convention to assign "checked". Most importantly, strings like "true" or "false" don't have any special meaning.

If you don't aim for XHTML conformance, you can simplify the code to:

<input type="radio" name="imgsel" value="" checked>
Millennial answered 8/4, 2011 at 8:19 Comment(6)
Duplicate of #4711536Thyroiditis
It is just as important to know the gotcha with this: You need to make sure NO other inputs in this radio button group contain the markup checked="false", nor "checked" by itself. Otherwise, the last one of these that appears on the page will be checked. Only the one you want selected by default should have any markup that contains "checked".Cultivable
If the problem persists, you should add the attribute autocomplete="off" to force the browser to set the default value.Soupspoon
"Empty attribute" (still) valid in the current HTML 5.2 recommendation.Anemography
which is better checked="checked" or just checked?Chungchungking
@OMNIADesignandMarketing checked="checked" is better, because, it is accepted in both HTML and XHTML. And it looks better than empty checked attribute!While
I
133

Use the checked attribute.

<input type="radio" name="imgsel"  value="" checked /> 

or

<input type="radio" name="imgsel"  value="" checked="checked" /> 
Incrust answered 8/4, 2011 at 8:19 Comment(6)
<input type="radio" name="imgsel" value="" checked="false" /> also causes the button to be checked.Viddah
I suggest only using checked or checked="checked" and vehemently avoid using "true" as it implies that "false" will do the opposite... which it doesn't.Vitric
The first choice is the right answer. Boolean attributes should not have values.Did
@JasonTu Both are correct. XHTML requires the second version, while HTML allows for either.Skiagraph
does that mean that checked="checked" is better?Chungchungking
No, just that it's more backward compatible. But XHTML is sorta dead anyway...Prostyle
U
83

This doesn't exactly answer the question but for anyone using AngularJS trying to achieve this, the answer is slightly different. And actually the normal answer won't work (at least it didn't for me).

Your html will look pretty similar to the normal radio button:

<input type='radio' name='group' ng-model='mValue' value='first' />First
<input type='radio' name='group' ng-model='mValue' value='second' /> Second

In your controller you'll have declared the mValue that is associated with the radio buttons. To have one of these radio buttons preselected, assign the $scope variable associated with the group to the desired input's value:

$scope.mValue="second"

This makes the "second" radio button selected on loading the page.

EDIT: Since AngularJS 2.x

The above approach does not work if you're using version 2.x and above. Instead use ng-checked attribute as follows:

<input type='radio' name='gender' ng-model='genderValue' value='male' ng-checked='genderValue === male'/>Male
<input type='radio' name='gender' ng-model='genderValue' value='female' ng-checked='genderValue === female'/> Female
Unjaundiced answered 24/11, 2015 at 14:46 Comment(5)
I was surprised that the accepted answer didn't work - until I read your comment. I too am using AngularJS, and this worked a treat!Federalize
In my case, also using angularjs, I had value but I had to change it to ng-value for it to work properly.Endodermis
Ah yes. It should be noted that this is an answer for angular 1.x not greaterUnjaundiced
This answer should be deleted because this answer doesn't answer the question.Goshen
Yes I'm using AngularJS and previous answers not worked, I tried your second and it worked.Valentijn
I
31

Add this attribute:

checked="checked"
Itin answered 8/4, 2011 at 8:20 Comment(0)
T
20

They pretty much got it there... just like a checkbox, all you have to do is add the attribute checked="checked" like so:

<input type="radio" checked="checked">

...and you got it.

Cheers!

Triny answered 8/4, 2011 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.