I put a portion of my code on a codepen for an example:
What I’m trying to do is display these variable values in a table. When information is entered into the first form, it’s meant to subtract that value from the variable and update in the table. When information is entered into the second form, it’s meant to add that value to the variable and update in the table. (I’m making a small inventory tracking site.)
Both forms use input fields, and the type is set to number. I’ve got the variable initialized with a number.
Here’s what’s happening: the first form works as I want, and the value entered subtracts from the variable and shows in the table. All good. However, when I enter values into the second form, it’s somehow changing from a number into a string, and so it’s appending the entered value to the end of the variable instead of changing/adding the value to the variable. And I can’t for the life of me figure out why.
Both functions for the forms are almost identical…the only differences (at least that I can tell) are the names of the functions and local variables, and that one does -= and one does +=. I’m really hoping someone can help me figure out why the second form is concatenating and not adding, cause I can’t see it. Why would one work and the other not - is it only that js also uses the + sign to concate, and so I need to do something else? I’m checking each input to tell if it’s empty or not, and if it is assigning it to 0 - is there maybe something going weird with that that changes it from number to string, or maybe not even necessary? I could really use some fresh eyes to hopefully help me figure out why this change is happening and how I can fix it.