1

I need to check if any checkbox is checked. so i am doing it like

self.isButtonEnabled = function() {
  var selectLineCheckboxs = document.getElementsByClassName('selectLineRadioInput'), 
  i = 0, checkboxLength = selectLineCheckboxs.length - 1;

  for (i = 0; i <= checkboxLength; i++) {
    if (selectLineCheckboxs[i].checked) {
      self.selectLineChecked = true;
      break;
    } else {
      self.selectLineChecked = false;
    }
  }
  return self.selectLineChecked;
};

in return i get true if any checkbox is checked.

so quite simple,

Now here i am looking if we can do the same with angularJs with any better approach and i do not want to use watch() function in angular.

2 Answers 2

2

I can help with your some code to convert it to look like in Angular way.

  1. use angular.element (provided by jQLite to get element) as instead of document.getElementsByClassName
  2. You could use $filter while checking attribute is checked or not

CODE

self.isButtonEnabled = function() {
    var selectLineCheckboxs = angular.element('.selectLineRadioInput');
    var checkedValues = $filter('filter')(selectLineCheckboxs, { 'checked': true }); //do filtering and contains check value
    self.selectLineChecked = checkedValues.length > 0 ? true : false;
    return self.selectLineChecked;
};

Note: You should add $filter dependency on your controller before using $filter

Update

I'd suggest you to create your own custom filter that could be usable in multiple purposes, or dynamically check property value is true or not. I know your code is as same as you ask in answer, but I putted some of your code as reusable component, which can dynamically work for any property value to check is true or not.

Filter

.filter('isPropertyTrue', function () {
    return function (elements, property) {
        var returnArray = [];
        angular.forEach(elements, function (val, index) {
            if (val[property]) returnArray.push(val)
        });
        return returnArray;
    }
});

Code

$scope.isButtonEnabled = function () {
    var selectLineCheckboxs = document.getElementsByClassName('selectLineRadioInput');
    var checkedValues = $filter('isPropertyTrue')(selectLineCheckboxs, 'checked');
    self.selectLineChecked = checkedValues.length > 0 ? true : false;
    return self.selectLineChecked;
};

JSFiddle Hope this could help you, Thanks.

Sign up to request clarification or add additional context in comments.

3 Comments

@mayank does it helpful to you?
This line has issue $filter('filter')(selectLineCheckboxs, { 'checked': true }); this returns all checkbox but not all those checked !!
@downvoter could I know the reason behind down vote?
0

i have been using it this way with ng-bind variable in scope $scope.Items .and the binded variable can be used to see what all items are checked.

 angular.forEach($scope.Items,function(key,value)
                  {          
                                if(key.Selected)
                                {
                                    counter++;
                                }
                     });

Here is a JSFiddle to illustrate the same

3 Comments

Number of checkbox i am getting from API which is dynamic.so i cant set $scope.Items name obj
if you can use jQuery may be this would be helpful $('form input[type=checkbox]:checked').size()
I cant use jquery only javascript only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.