0

I want to add checked and unchecked attribute to the checkbox which is in a table I tried to using it via jquery in angularjs

$('#chksitecolumn_' + item.Id).attr('checked', true)

Here item.Id is the id which I am iterating and binding it on ng-model.

and also tried it using angularjs

var element = angular.element('#chksitecolumn_' + item.Id);
element.attr('checked', 'checked');

but it is not working

Here is my checkbox

<input type="checkbox" class="test"  id="chksitecolumn_{{item.Id}}"
       ng-model="selected[item.Id]"/>

I am passing an ID as a key using ng-model I want to know that can we check uncheck checkbox except using ng-model?

3
  • 1
    Why don't you want to use ng-model?
    – Bill P
    Commented Feb 28, 2020 at 15:09
  • I am using ng model to pass the Id i know it can be done using ng-model but want to know can we check unckeck it except using ng-model Commented Feb 28, 2020 at 15:11
  • You should probably not mix AngularJS and jQuery, but if you must, use prop('checked', true), not attr('checked', true) or attr('checked', 'checked'). See "Attributes vs. Properties" in the documentation. Commented Feb 28, 2020 at 15:33

1 Answer 1

1

To check/uncheck you can use ng-checked directive (documentation) :

<input type="checkbox" class="test" id="chksitecolumn_{{item.Id}}"
       ng-checked="checked[item.Id]" />

Note that you should not use ng-checked with ng-model together:

Note that this directive should not be used together with ngModel, as this can lead to unexpected behavior.

3
  • And how can i bind it in angular Commented Feb 28, 2020 at 16:15
  • @Gaurav_0093 Use ng-checked for one-way binding. Use ng-model for two-way binding. Don't use the ng-checked and ng-model directives together on the same input element.
    – georgeawg
    Commented Feb 28, 2020 at 16:17
  • @Gaurav_0093 You can set the assigned variable as true/false from the controller
    – Bill P
    Commented Feb 28, 2020 at 18:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.