Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding values from two separate functions in javascript

Status
Not open for further replies.

aspire011

MIS
Dec 18, 2013
5
0
0
BB
I want to call a function that adds values from two separate functions. the functions produce values in a drop down box. For example function one outputs dropdon value in a text field box and function two outputs another value .
I want to call a function that adds the values of the two drop down boxes. The code for each dropdown is in a separate function.
My problem is adding the values from the two dropdown boxes.
Here is my code


<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">

var dropdown1,field,dropdown2,result;

function Foodplace()
{
dropdown1 = document.getElementById("ifastfood");
field = document.getElementById("ifastfoodName");
field.value = dropdown1.value;
}

function PopulateUserName2()
{
dropdown2 = document.getElementById("ifastfood2");
field = document.getElementById("ifastfoodName2");
field.value = dropdown2.value;
}


function calculate(){
result=dropdown1.value+dropdown2.value;
document.write("result");

}


</script>
</head>
<body>
<select name="nfastfood" id="ifastfood" onchange="Foodplace()">
<option value=>Select an item </option>
<option value=8.95>potato roti </option>
<option value=9.95>Beef roti</option>
</select>
<input id="ifastfoodName" name="OBKey_WF_Manger_Supervisor_1" type="text" />
<br><br>

<select name="nfastfood2" id="ifastfood2" onchange="PopulateUserName2()">
<option value=>Select an item </option>
<option value=11.56>Bran Burger </option>
<option value=12.33>Burger with cheese</option>
</select>
<input id="ifastfoodName2" name="OBKey_WF_Manger_Supervisor_1" type="text" />
<br><br>

<button onclick="add()">Calculate</button>
</body>
</html>



















 
the function calculate


function calculate(){
result=dropdown1.value+dropdown2.value;
alert(result;

}


is working except that it prints out the numbers as if they were text and did not add them. So the program is not seeing the dropdown values as numbers. My guess is if it sees them as number the calculation would be correct. So how do I format the dropdown values as numbers and not text.
 
I have to change the var to number outside of the functions so that they can be read from any where in the program

eg dropdown1=Number(dropdown1).value;
dropdown2=Number(dropdown2),value;

the program is still reading the numbers as text

Tell me where I am going wrong please.
 
WORKED WITH THESE CHANGES
Strange that the number function did not work. It works in a single function, when I tried to use it my select boxes would not work, but I figured it out.

Here are the changes. Its now working.

function calculate(){
result=parseInt(dropdown1.value) + parseInt(dropdown2.value);

document.getElementById("answer").value=(result.toFixed(2));
}

I sent the answer to a text box instead of a document.write

so after the button onclick I put my text box

<input id="answer">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top