Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I am new to html and I am trying to write a simple calculator that will pass input values from a form to a function, do a basic IF test, do a calculation based on IF outcome and pass back result to form.

Below is my code but it doens't seem to work and I cannot see why. Please assist.

HTML
<!DOCTYPE html>
<html>
<head>

<body>

<form>Value One:     <input type="text" name="valueOne"   id="valueOne">   <br>
      Value Two:     <input type="text" name="valueTwo"   id="valueTwo">   <br>
      Value Three:   <input type="text" name="valueThree" id="valueThree"> <br>

      <input type="submit" value="Submit" onclick="myCalc(document.getElementById('valueOne').value, document.getElementById('valueTwo').value), document.getElementById('valueThree').value)">
</form>

<script>

function myCalc(valueOne, valueTwo, valueThree)
{
    if (valueOne.value = 0) {
        alert(document.getElementById('valueTwo').value + document.getElementById('valueThree').value);
    } else if (valueTwo.value = 0) {
        alert(document.getElementById('valueOne').value + document.getElementById('valueThree').value);
    } else  {
        alert(document.getElementById('valueOne').value)+ document.getElementById('valueTwo').value);
    }
}

</script>

</body>
</html>
Posted

Hi,

I have correct your code. Please see below.

HTML
<!DOCTYPE html>
<html>
<head>
 
<body>
 
<form>Value One:     <input type="text" name="valueOne"   id="valueOne">   <br>
      Value Two:     <input type="text" name="valueTwo"   id="valueTwo">   <br>
      Value Three:   <input type="text" name="valueThree" id="valueThree"> <br>

<div id="result"></div>
 
      <input type="button" value="Submit"  önclick="javascript:myCalc(document.getElementById('valueOne').value, document.getElementById

('valueTwo').value, document.getElementById('valueThree').value)">
</form>
 
<script>
 
function myCalc(valueOne, valueTwo, valueThree)
{
    if (valueOne == 0) {
        document.getElementById("result").innerHTML = 'Result: '+ (parseFloat(valueTwo) + parseFloat(valueThree));
    } else if (valueTwo == 0) {
        document.getElementById("result").innerHTML = 'Result: '+ (parseFloat(valueOne) + parseFloat(valueThree));
    } else  {
        document.getElementById("result").innerHTML = 'Result: '+ (parseFloat(valueOne) + parseFloat(valueTwo));
    }
}
 
</script>
 
</body>
</html>
 
Share this answer
 
v2
Thanks for the reply Ranjan

But your reply was only based on integer values and I need to work with dbl values.
How would this change the code?

Also how would the code change if the output was not in an alert but an output on the form?
 
Share this answer
 
Comments
Ranjan.D 15-May-14 9:58am    
I have updated the code. Please see the above code. For more information on the basics, please do refer http://www.w3schools.com/jsref/jsref_parsefloat.asp
JacquesGrobler 16-May-14 1:07am    
Hi Ranjan

Thanks for the changes but now nothing happens on my side.
I input the values, press calc and then nothing
JacquesGrobler 16-May-14 2:13am    
Ok, I found the problem, the error was on the "onclick": ö

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900