# Trouble with JavaScript Function

• Born
• Posts: 2

3+ Months Ago

I am trying to get the following function to calculate a subtotal by product and then call a calcTotal() function. The input fields are ordered 1 through 6. The field names are price1, qty1, and cost1 and so on for each of the six products. The function is supposed to display the costX amount in the costN field. The input parameter of item is a whole number (integer of 1,2,3,4,5, or 6).

When I hard code the number it works great, when I try to pass the the item nothing happens.

HELP!

Code: [ Select ]

function calcCost(item)
{
// declare variables
var priceX = parseFloat(document.orders.price(item).value);
var qtyX = parseFloat(document.orders.qty(item).value);
var costX = parseFloat(document.orders.cost(item).value);

/* Regular expression for a text string containing 1 or more digits and
no other characters. This expression recongizes integers only. Other
combinations such as 9x, a18, 24-b, 18.2, 24.8 etc. are rejected. */
var reqty = /^\d+\$/;
if (reqty.test(document.orders.qty1.value) == false)
{
alert("You must enter qty as a whole number of 0 or more.");
document.orders.qty(item).value == 0;
document.orders.qty(item).focus();
document.orders.qty(item).select();
calcTotal();
}
else
{
costX = (priceX * qtyX).toFixed(2);
document.orders.cost(item).value = costX;
}
calcTotal();
}

1.
2. function calcCost(item)
3.   {
4.   // declare variables
5.   var priceX = parseFloat(document.orders.price(item).value);
6.   var qtyX = parseFloat(document.orders.qty(item).value);
7.   var costX = parseFloat(document.orders.cost(item).value);
8.
9.     /* Regular expression for a text string containing 1 or more digits and
10.      no other characters. This expression recongizes integers only. Other
11.      combinations such as 9x, a18, 24-b, 18.2, 24.8 etc. are rejected. */
12.   var reqty = /^\d+\$/;
13.   if (reqty.test(document.orders.qty1.value) == false)
14.     {
15.     alert("You must enter qty as a whole number of 0 or more.");
16.     document.orders.qty(item).value == 0;
17.     document.orders.qty(item).focus();
18.     document.orders.qty(item).select();
19.     calcTotal();
20.     }
21.   else
22.     {
23.     costX = (priceX * qtyX).toFixed(2);
24.     document.orders.cost(item).value = costX;
25.     }
26.   calcTotal();
27.   }
28.

## Post Information

• Total Posts in this topic: 1 post
• Users browsing this forum: No registered users and 33 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum