I want to set default value of checkbox to checked ie., user must see that check box is checked when opening the page for some reason it does not work while using angularjs
here is my code
<html ng-app>
<input type="radio" name="lookup" ng-model="lookup" value="1" ng-checked="lookup==1" checked>Employee Lookup</input>
<input type="radio" name="lookup" ng-model="lookup" value="2" ng-checked="lookup==2">Company Lookup</input>
</html>
The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
Since you're using ngModel
and value
, the radio will automatically be selected if they match. Here is the HTML and JS:
<html ng-app ng-controller="testcontroller">
<input type="radio" ng-model="lookup" value="1">Employee Lookup</input>
<input type="radio" ng-model="lookup" value="2">Company Lookup</input>
</html>
function testcontroller($scope){
$scope.lookup = 1;
}
And here is a working jsFiddle demonstration: http://jsfiddle.net/BinaryMuse/ZQDts/3/ (You might have been having trouble with your jsFiddle because you misspelled ng-app
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With