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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Don't know why this function is not working 1

Status
Not open for further replies.

haneen97

Programmer
Dec 10, 2002
280
US
It was working fine until I added the alert code. I can't seem to see anything that would stop it from running. Can you please take a look? I get an error on the status bar on the bottom of the page that says "error with page".

function ItemTotal(which,txtValue)
{
var Qty=eval(which.value);
var PrevTotal=eval(document.getElementById('txtTotal').value) * 1;
var PrevSubTotal=eval(document.getElementById('txtSubTotal').value) * 1;
var CurrReOrd=eval(document.getElementById(txtValue+'ReOrder').value) * 1;
var PrevQtyOnHand=eval(document.getElementById(txtValue+'QtOnHand').value) * 1;
var unitprice=eval(document.getElementById(txtValue+'UPrice').value) * 1;

var NewQtyOnHand=PrevQtyOnHand - Qty;

if (Qty > PrevQtyOnHand)
{
alert('Not Enough items in stock to fulfill this order. There are/is only '+PrevQtyOnHand+' items in stock');
return (false);
}
else
if (NewQtyOnHand <= CurrReOrd)
{
alert ('ReOrder point has been reached for this product. There is enough in stock for this order');
return (false);
}
else
{
var tot = Qty * unitprice;
document.getElementById(txtValue+'QtOnHand').value = NewQtyOnHand;
document.getElementById(txtValue+'Total').value = tot;
document.getElementById('txtSubTotal').value = PrevSubTotal + tot;
document.getElementById('txtTotal').value = PrevTotal + tot;
return (true);
}
}



Thanks

Mo
 
First, get rid of all the eval() statements. You don't need them, and can access the values of the form elements directly with what you put inside the eval statements.

Not seeing the form elements themselves, this looks like it might be wrong:

document.getElementById(txtValue+'[red]QtOnHand[/red]').value

Should that by QtyOnHand

?

You have it spelled like that twice in your code, but spell the variable you put the value in with a "y".

Lee

 
Why doesn't anyone ever posts to say they don't know why a function IS working???
 
Excellent job Troll. The misspelled field id's were the problem. Thanks a lot.

Mo
 
You are right Sheco. Long day and too much looking at code.

Mo
 
I took them out cLFlaVA. What are they supposed to do any way?

Mo
 
That was the first thing I mentioned in my answer because it processes whatever the string is as if it were an actual written line of Javascript code.

If you have

var x = eval('1 + 2 * 3')

you'll assign the number 7 to the variable x. If you have a text input where someone can enter a math problem, then you'd use eval(document.getElementById('mathproblem').value) to run whatever was entered and produce the answer. Since the values you're getting from the inputs are (presumed to be) numbers, then you don't need to evaluate them as anything else.

eval() really slows things down, too, and I think it's because it spawns a separate Javascript process to evaluate the string each time you call eval(), which effectively stops the current code being processed until the spawned process is done running.

I'm sure someone can correct me on that, if I'm wrong. :)#

Lee
 
it was initially invented by someone in an effort to make javascript newcomers believe there was actually a function that would be needed for every single line of JavaScript code ever written.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
According to Microsoft's JScript help files:

Description
Evaluates JScript code.
Syntax
eval(codestring)
The codestring argument is a String object that contains valid JScript code. This string is parsed by the JScript parser and executed.

Remarks
The eval function allows dynamic execution of JScript source code. For example, the following code creates a new variable mydate that contains a Date object:

[red]eval[/red]("var mydate = new Date();");

The code passed to the eval method is executed in the same context as the call to the eval method.

Lee
 
Thank you all for providing this great information.

Mo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top