-1

how to add two variables, I'm doing it like this and they are concatenating

id('total-quotas').addEventListener('keyup', function () {
           id('vl_total').value = (id('total-quotas').value + id('valor6').value);
        });
  • convert values to integers ! with parseInt – Pierre Feb 12 '20 at 20:09
  • @epascarello another very relevant dupe https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum – VLAZ Feb 12 '20 at 20:13

2 Answers2

1

They are concatenating because they are strings.

If you expect those two values to be numeric, then cast them via parseInt.

parseInt(id('total-quotas').value, 10) + parseInt(id('valor6').value, 10)
alex067
  • 3,159
  • 1
  • 11
  • 17
0

Are these strings? If they are strings they will contact, if they are integers passed as strings you might need to parse as an integer

See https://www.w3schools.com/jsref/jsref_parseint.asp

Kevin W
  • 158
  • 1
  • 9