...

View Full Version : Passing values to a function



dlg0351
03-16-2005, 08:17 PM
I am trying to create a function that will calculate the cost of an item based on a quantity entered and the price per item. I want to reuse this function multiple times because there will be multiple items on the final form. Here is my code:



<html>
<head>
<title></title>

<script language="Javascript">
<!--
function calcTotal()
{
var prod_price = 38.75;
var prod_total = prod_price * document.order.prod1_qty.value;
document.order.prod1_sub.value = prod_total;
}
//-->
</script>
</head>

<body>

<form name="order">
<p>Item#01</p>
<p>Price: $38.75</p>

<p>Quantity:
<input type="text" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal();"></p>

<p>Total: $
<input type="text" name="prod1_sub" size=7></p>
</form>

</body>
</html>


Obviously this code works, but I am not sure how to manipulate the code to pass the necessary information to the function so it will work for multiple calculations.

Any help is greatly appreciated.

_Aerospace_Eng_
03-16-2005, 08:27 PM
try it like this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>

<script language="Javascript">
<!--
function calcTotal(what,which1,which2)
{
var prod_price = what;
var prod_total = prod_price * document.getElementById(''+which1+'').value;
document.getElementById(''+which2+'').value = prod_total;
}
//-->
</script>
</head>

<body>

<form name="order">
<p>Item#01</p>
<p>Price: $38.75</p>

<p>Quantity:
<input type="text" id="prod1_qty" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('38.75','prod1_qty','prod1_sub');"></p>

<p>Total: $
<input type="text" id="prod1_sub" name="prod1_sub" size=7></p>
<p>Item#02</p>
<p>Price: $99.99</p>

<p>Quantity:
<input type="text" id="prod2_qty" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('99.99','prod2_qty','prod2_sub');"></p>

<p>Total: $
<input type="text" id="prod2_sub" name="prod1_sub" size=7></p>
</form>

</body>
</html>

vwphillips
03-16-2005, 08:30 PM
<html>
<head>
<title></title>

<script language="Javascript">
<!--
var prod_price,prod_total;

function calcTotal(from,to){
if (isNaN(document.order[from].value)){
alert('Is Not a Number');
return;
}
prod_price = 38.75;
prod_total = prod_price * document.order[from].value;
document.order[to].value = prod_total;
}
//-->
</script>
</head>

<body>

<form name="order">
<p>Item#01</p>
<p>Price: $38.75</p>

<p>Quantity:
<input type="text" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('prod1_qty','prod1_sub');"></p>
<p>
<input type="text" name="prod2_qty" size=3 maxlength=2 onkeyup="calcTotal('prod2_qty','prod1_sub');"></p>

<p>Total: $
<input type="text" name="prod1_sub" size=7></p>
</form>

</body>
</html>

dlg0351
03-16-2005, 09:02 PM
Thanks for the replys. Unfortunately I can use the PHP code because the my client is not paying for PHP as part of their hosting package, if they were this would make things much easier.

As far as the code from _Aerospace_Eng_, it works great! My only concern is that I know getElementById does NOT work in older Netscape browser versions.

Any other thoughts?

Are there other ways to make reference to the names of the inout fields?

_Aerospace_Eng_
03-16-2005, 09:07 PM
you could probably modify it to use document.layers which i believe does work for the older browsers, but i wouldn't worry about it, NS4 is "dead", and vwphillips solution isn't php, its javascript, he just used the php vb code for the syntax coloring, so his way may work also with a slight modification of the price_value

Brandoe85
03-16-2005, 09:11 PM
vwphillips's example is javascript, maybe you should try it out :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum