Jump to content

[SOLVED] Variable being treated as string rather than integer.


Niccaman

Recommended Posts

	var cut1 = document.getElementById('input1').value;
var cut2 = document.getElementById('input2').value;
var cut3 = document.getElementById('input3').value;
var cut4 = document.getElementById('input4').value;
var total = cut1+cut2+cut3+cut4;

 

Why does this output '050500'

when entering 0,50,50,0 in the input fields?

Because they're all strings.

 

Try this -

   var cut1 = parseInt(document.getElementById('input1').value);
   var cut2 = parseInt(document.getElementById('input2').value);
   var cut3 = parseInt(document.getElementById('input3').value);
   var cut4 = parseInt(document.getElementById('input4').value);
   var total = cut1+cut2+cut3+cut4;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.