Q:

updating text using knockout computed function

In a table I have a checkbox bound to a bool in an observable array.
If any of the checkboxes in the table are checked / unchecked I want to update some text with the total checked.
I cannot get the computed function to fire, I have tried using ko.utils.unwrapObservable on both the array and location.isSelected in the 'if' statement below, am I just using it in the wrong place?
<input type="checkbox" data-bind="checked: isSelected"/>

<span class="text-left h5 ">Total Selected:</span><span data-bind="text: totalSelected" /> 

self.totalSelected = ko.computed(function () {
    var selected = 0;
    ko.utils.arrayForEach(self.SelectedLocations(), function (location) {
        if (location.isSelected == true) {
            selected = (+selected) + 1;
        }
    });
    return selected;
}, self).extend({ notify: 'always' });

A:

One of the issues is that isSelected is treated like a variable inside the computed: location.isSelected == true. However, if you intend to bind a checkbox to it, it must be an observable.
So, I have declared a function to create the children of self.SelectedLocations as:
var locationObservable = function() {
  var self = this;
  self.isSelected = ko.observable(false);
};

Then, you could change the counting in the computed variable as follows:
if (loc.isSelected()) {
    selected++;
  }

var locationObservable = function(selected) {
  var self = this;
  self.isSelected = ko.observable(selected);
};

var model = function() {
  var self = this;

  self.SelectedLocations = ko.observableArray();
  self.SelectedLocations.push(new locationObservable(false)); // Set the state of the checkbox here.
  self.SelectedLocations.push(new locationObservable(true));
  self.SelectedLocations.push(new locationObservable(false));

  self.totalSelected = ko.computed(function() {
    var selected = 0;
    ko.utils.arrayForEach(self.SelectedLocations(), function(loc) {
      if (loc.isSelected()) {
        selected++;
      }
    });
    return selected;
  }, self);
};

var vm = new model();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<div data-bind="foreach: SelectedLocations">
  <input type="checkbox" data-bind="checked: isSelected" />
</div>


<span class="text-left h5 ">Total Selected:</span><span data-bind="text: totalSelected" />

