RealTime Sum of 3 fields output on 4th field


#1

Hello i´m a beginner in javascript

This works with 2+2+2=6

but not work with 2.5 +2.5+ 2.5 =7.5

<input type="text" id="my_input1" value=""/>
<input type="text" id="my_input2" value="" />
<input type="text" id="my_input3" value="" />
<input type="text" id="result" value="" onfocus="doMath();" />


<script type="text/javascript">
    function doMath()
    {
        // Capture the entered values of two input boxes
        var my_input1 = document.getElementById('my_input1').value;
        var my_input2 = document.getElementById('my_input2').value;
        var my_input3 = document.getElementById('my_input3').value;

        // Add them together and display
        var sum = parseInt(my_input1) + parseInt(my_input2)  + parseInt(my_input2);
        document.getElementById('result').value =(sum);
    }
</script>	
</body>
</html>

#2

solution in jquery here http://jsfiddle.net/heera/esqnC/6/


#3

So you fixed it by using float instead of int? Why not just post the code here instead of making us go out to another page. You did post the original here!

Anyway, glad you solved it.


#4

I just put the link because I found it,

include jquery

function add(){
        var first=parseFloat($("#first").val());
        var second=parseFloat($("#second").val());
        $("#result").val(+(first+second).toFixed(2));
}

add();
$("input").blur(add);

console.log(+(0.1 + 0.2).toFixed(12) );