Question

I have this code that add numbers and prints it in input text for readonly

document.getElementById('total') += parseInt(tot);

but it JUST adds numbers as sting , for example when add 8 and 10 that would be 18 but it prints them as 810 , why ?

Était-ce utile?

La solution 3

You can try this:

var elem = document.getElementById('total');
elem.value = +elem.value + parseInt(tot);

The + sign force the type to number.

Autres conseils

Use the parseInt() function to both of them:

document.getElementById('total').value = parseInt(tot)+parteInt(document.getElementById('total').value);

Just parse the value from string to integer like this

var x = document.getElementById('total');    
x.value = parseInt(x.value) + parseInt(tot);

Or use parseFloat() if you have decimal numbers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top