RealTime Sum of 3 fields output on 4th field

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>

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

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.

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) );
Sponsor our Newsletter | Privacy Policy | Terms of Service