Help with summing cells in grid using Selectable

Help with summing cells in grid using Selectable

I am trying to implement a function like the Excel function of summing cells in a grid by dragging a fence around the cells. The code below only outputs the last cell selected:

  1. $(function() {
      $('#Grid1').selectable({
        selected: function(event,ui) {
          var res = 0;
          $(ui.selected, this).each(function() {
          var nocomma = ui.selected.innerHTML.replace(/,/g, '');
         
          res += parseFloat(nocomma);
          changeElementValue('sum',res);
          });
        }
      });
    });