Assign an initial value to radio button as checked
Asked Answered
C

11

181

How do I assign the value of a radio button initially as checked in HTML?

Conformance answered 17/1, 2011 at 7:49 Comment(0)
E
214

You can use the checked attribute for this:

<input type="radio" checked="checked">
Expediency answered 17/1, 2011 at 7:52 Comment(3)
you can also just mention that attribute with no value assigned i.e.<input type="radio" checked>Paviour
@Paviour I believe checked="checked" is the valid way to pre-check a radio button - just using "checked" isn't valid HTML (despite being supported by most browsers)Kyleekylen
@matthewh - nah, you can use just "checked" on it's own and it is valid HTML (although not valid XHTML). Personally I prefer 'checked="checked"', but you don't have to use it ... indeed, there's quite a strong case against using it.Konikow
A
65

You can just use:

<input type="radio" checked />

Using just the attribute checked without stating a value is the same as checked="checked".

Asthma answered 17/1, 2011 at 9:38 Comment(2)
as @Matt Healey stated, using checked without the attribute is not valid HTMLBlankly
@Blankly incorrect. it is not valid Xhtml, however it is completely valid for HTML5 which is as global of a standard as anything will ever be.Roofing
K
16

I've put this answer on a similar question that was marked as a duplicate of this question. The answer has helped a decent amount of people so I thought I'd add it here too in just in case.

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.

Keek answered 4/4, 2016 at 14:22 Comment(1)
Indeed! The above answers don't work with Angular JS. Your answer resolved my issue. Thanks! :)Challis
C
4

For anyone looking for an Angular2 (2.4.8) solution, since this is a generically-popular question when searching:

<div *ngFor="let choice of choices">
  <input type="radio" [checked]="choice == defaultChoice">
</div>

This will add the checked attribute to the input given the condition, but will add nothing if the condition fails.

Do not do this:

[attr.checked]="choice == defaultChoice"

because this will add the attribute checked="false" to every other input element.

Since the browser only looks for the presence of the checked attribute (the key), ignoring its value, so the last input in the group will be checked.

Condescendence answered 23/2, 2017 at 21:54 Comment(0)
I
3

Note that if you have two radio button with same "name" attribute and they have "required" attribute, then adding "checked" attribute to them won't make them checked.

Example: This makes both radio button remain unchecked.

<input type="radio" name="gender" value="male" required <?php echo "checked"; ?>/>
<input type="radio" name="gender" value="female" required />

This will makes the "male" radio button checked.

<input type="radio" name="gender" value="male" <?php echo "checked"; ?>/>
<input type="radio" name="gender" value="female" />
Interlunar answered 13/8, 2017 at 12:39 Comment(0)
S
2

The other way to set default checked on radio buttons, especially if you're using angularjs is setting the 'ng-checked' flag to "true" eg: <input id="d_man" value="M" ng-disabled="some Value" type="radio" ng-checked="true">Man

the checked="checked" did not work for me..

Sophiesophism answered 10/11, 2016 at 19:15 Comment(2)
cause ng-checked="true" is from angularjs repertoire.Ative
Or set the model value in the controller to "true". Watch out that you set it to a string, not a Boolean!Mussorgsky
U
1

Im a bit late to the party and I know the OP said html, but if you needed to do this in MVC you can set true in the third param.

eg:

    <p>Option One :@Html.RadioButton("options", "option1", true})</p>
    // true will set it checked

     <p>Option Two :@Html.RadioButton("options", "option2"})</p>
     //nothing will leave it unchecked
Ulberto answered 23/5, 2018 at 21:18 Comment(0)
L
0

If you are using react-redux for your application and if you want to show data which is in the redux store, you can set "checked" option as below.

<label>Male</label>

 <input
   type="radio"
   name="gender"
   defaultChecked={this.props.gender == "0"}
 />



 <label>Female</label>
 <input
   type="radio"
   name="gender"
   defaultChecked={this.props.gender == "1"}
 />
Lavonda answered 17/7, 2019 at 7:52 Comment(0)
A
0

What worked for me, was assigning the same name attribute to the radio buttons as for the containing form tags, and the checked="checked" attribute for the default button, as shown below:

<form name="State" method="get" action="">
    <input type="radio" name="State" id="" value="" />
    <input type="radio" name="State" id="" value=""checked="checked"/>
    <input type="radio" name="State" id="" value="" />
    <input type="radio" name="State" id="" value="" />
</form>

In the example above, the second radio button will be checked when the page is loaded.

Alcaide answered 15/8, 2022 at 21:48 Comment(0)
S
0

The initial "checked" property is only working, if not empty values are applied to all radio buttons:

<form>
    <input type="radio" name="myRadioGroup" value="1" >
    <input type="radio" name="myRadioGroup" value="2" checked >
    <input type="radio" name="myRadioGroup" value="3" >
    <input type="radio" name="myRadioGroup" value="4" >
</form>

Do not ask me why. But that solved that problem.

Stereophotography answered 23/11, 2022 at 14:22 Comment(0)
F
0

In React or Next, do this.

<fieldset>
  <input  value='value' type="radio" name='myfield" defaultChecked />
  <input  value='value' type="radio" name='myfield" />
</fieldset>
Finney answered 8/5, 2023 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.