Assign an initial value to radio button as checked

HtmlRadio Button

Html Problem Overview


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

Html Solutions


Solution 1 - Html

You can use the checked attribute for this:

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

Solution 2 - Html

You can just use:

<input type="radio" checked />

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

Solution 3 - Html

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.

Solution 4 - Html

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" />

Solution 5 - Html

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..

Solution 6 - Html

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.

Solution 7 - Html

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

Solution 8 - Html

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"}
 />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRPBView Question on Stackoverflow
Solution 1 - HtmlpolarblauView Answer on Stackoverflow
Solution 2 - HtmlseedgView Answer on Stackoverflow
Solution 3 - HtmldiscodaneView Answer on Stackoverflow
Solution 4 - HtmlYZ ZheView Answer on Stackoverflow
Solution 5 - HtmlDeepika PethaperumalView Answer on Stackoverflow
Solution 6 - HtmlmsanfordView Answer on Stackoverflow
Solution 7 - Htmluser2903379View Answer on Stackoverflow
Solution 8 - HtmlRuksView Answer on Stackoverflow