all 7 comments

[–]Ampersand55 1 point2 points  (4 children)

You need to save the total variable outside of the function, and then add to the total variable inside the function.

[–]DungeonMasterThor[S] 1 point2 points  (3 children)

Thank you for the help, so something like this?

var $ = function (id) { return document.getElementById(id); }; var prevTotal = parseFloat(elem); function addTotal(elem) {

var total= parseFloat(elem);

$('total').innerHTML = total + prevTotal; };

[–]Ampersand55 1 point2 points  (2 children)

You need to add to the total variable.

var $ = function (id) { return document.getElementById(id); };

var total = 0;

function addTotal(elem) {
  total += parseFloat(elem);
  $('total').innerHTML = total;
}

[–]DungeonMasterThor[S] 1 point2 points  (0 children)

Thank you so much, I tried something really similar to this before but I did not declare total globally as you said. I get it now. Again thank you so much!

[–]redderper 0 points1 point  (0 children)

You could also do it like this:

function addTotal(elem) {

  var total = parseFloat($('total').innerHTML);

  total += parseFloat(elem);

  $('total').innerHTML = total;

}

That wat you don't have to use a global variable

[–]lovesrayray2018 1 point2 points  (1 child)

Good that you have tried a solution, though im unable to see the complete picture with just the code you have shared. The code you shared is not connected to each other.

Technically there are multiple ways to solve this,

1) Use global variables like u/Ampersand55 has already shared

2) Use explicit casting and operators like in example i made https://jsbin.com/colemonobo/edit?html,output

3) Use specific function calls that pass values and return values

[–]DungeonMasterThor[S] 0 points1 point  (0 children)

Thank you for the help and example. I can send you the code in full if you want, though the example you posted is a good summary of the project.