Mindi Torrey Mindi Torrey - 1 year ago 65
HTML Question

How to set a default radio button with a value?

General issue: I cannot set a default radio button in my radio groups unless I remove the value attribute from the inputs.

Specifics: Each input has a value that is needed as it is being used by angular in other places in the app. So, I need to know how to set an input to default checked while retaining the values on the inputs. It has the same behavior if I try to add checked with jQuery instead of in HTML.

I have looked at several related questions including here. The specific issue is I need to have values and be able to set one input to checked as a default. The first input is set to checked, but it does not actually work unless I remove the value from that input.

Here is the HTML:

<div class="radio">
<input type="radio" ng-model="optionsRadios" id="new" value="new" checked>

<input type="radio" ng-model="optionsRadios" id="used" value="used">

<label >
<input type="radio" ng-model="optionsRadios" id="broken" value="broken">

Answer Source

You have to use ng-checked="true" instead of checked

<input type="radio" ng-model="optionsRadios" id="new" value="new" ng-checked="true">

<!DOCTYPE html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="">
<p>My cars:</p>
<input type="radio" ng-model="all" value="Volvo" ng-checked="true">Volvo<br>
<input type="radio" ng-model="all" value="Ford">Ford<br>
<input type="radio" ng-model="all" value="Mercedes">Mercedes