New Posts  All Forums:Forum Nav:

Javascript help

post #1 of 4
Thread Starter 
So I am doing a simple menu ordering for a javascript class but the only problem I have is that I need to add up three text fields which when you on click on a item it will have the cost (in one of the text fields) which contain decimals in them. I tryed parseFloat() and parseInt() but it doesn't work with decimals.

Is their a way to add numbers that have decimals in them?
post #2 of 4
You just have to convert them to decimals (doubles) right away.

Here is a full example
Code:
<head>
    <title>JS Example</title>
    <script type="text/javascript">
        function addThese(t1, t2, t3)
        {
            var v1 = parseFloat(document.getElementById(t1).value);
            var v2 = parseFloat(document.getElementById(t2).value);
            var v3 =  parseFloat(document.getElementById(t3).value);
            var total = v1+v2+v3;
            document.getElementById('output').innerHTML = total;
        }
    </script>
</head> 
<body>
    <input id="Text1" type="text" /><br />
    <input id="Text2" type="text" /><br />
    <input id="Text3" type="text" /><br />
    <input id="Button1" type="button" onclick="addThese('Text1','Text2','Text3')" value="button" />
    <div id="output"></div>
</body>
</html>
post #3 of 4
Thread Starter 
Thanks for your help.


+1 for helping me out.
post #4 of 4
No problem, glad to help out!
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding