1

Don't know why it doesn't work. jquery isn't adding them up. It displays just 00. I'm using rails 4.

# application.js 
var sum = 0;
$('.sum1').each(function() {
  sum += +$(this).text()||0;
});
$("#sum1_total").text(sum);


# view
<% @modification.boxes.each do |d| %>  
  <tr class="sum">
    <td><%= best_in_place d, :shares, :type => :input, :class => :sum1 %></td>
  </tr>
<% end %> 
<tr>
  <td><span id="sum1_total">00</span></td>
</tr>
DanielsV
  • 892
  • 1
  • 8
  • 26

1 Answers1

2

As written, the DOM elements are not accessible when the script executes. The result is that no elements are changed. Wrap your "application.js" in a dom ready callback function using jQuery's $(function{}) or native addEventListener with "load".

Travis J
  • 81,153
  • 41
  • 202
  • 273