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

Help with updating a text field

Status
Not open for further replies.

buba0418

Programmer
Apr 18, 2007
6
US
here's what I want to do...
I have a pricing table for figuring billing prices.
there are only a couple of fields involved in this table.

"PriceChange" This is the basePrice ofhow much
the price should jump per price change

"Line" This is the CostFactor the number to multiply the basePrice By.

"Fee" This is the final cost for the Line.

Recently they asked to be able to change the price half
way through the table and have the rest of the table reflect the new price.
On a short table like the one below, it's not a problem to manually change
the Fee, but if you have a table that has about 100 price changes
it can get tideous to update all the prices manually.

OK, in comes javascript.
I figured, I'd make a function that on change of the price code
it would figure out the new Fee. then do a for loop to update the rest.


I figured I could make the Document.form.Fee(CostFactor)>value update dynamically.
by creating the value Apt = "document.form.Fee" + CostFactor + ".value"
then the next line be Apt = Fee. This didn't work.

I verified the script works if I hardcode the document.formfee2.value like
document.form.Fee2.value = Fee;
See Below

How can I make the function PriceHandler1 work as described above?


============ CODE BELOW ===========
<HTML>
<HEAD><TITLE>TEST</HEAD>
<SCRIPT Language=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
function PriceHandler1(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = "document.form.Fee" + CostFactor + ".value"
Apt = Fee;
alert(ThsMsg);
}
function PriceHandler2(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = "document.form.Fee" + CostFactor + ".value"
document.form.Fee2.value = Fee;
alert(ThsMsg);
}
</SCRIPT>
</HEAD>
<BODY>

<FORM ACTION="" METHOD=POST NAME="form">
Line 1 <INPUT TYPE=TEXT NAME=PriceChange1 Value="150" onChange="PriceHandler2(1,document.form.PriceChange1.value)"> <INPUT TYPE=TEXT NAME=Fee1 VALUE="150"><BR>
Line 2 <INPUT TYPE=TEXT NAME=PriceChange1 Value="150" onChange="PriceHandler2(2,document.form.PriceChange2.value)"> <INPUT TYPE=TEXT NAME=Fee2 VALUE="300"><BR>
Line 3 <INPUT TYPE=TEXT NAME=PriceChange3 Value="150" onChange="PriceHandler2(3,document.form.PriceChange3.value)"> <INPUT TYPE=TEXT NAME=Fee3 VALUE="450"><BR>
Line 4 <INPUT TYPE=TEXT NAME=PriceChange4 Value="150" onChange="PriceHandler2(4,document.form.PriceChange4.value)"> <INPUT TYPE=TEXT NAME=Fee4 VALUE="600"><BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
 
Sorry Code Correction...
Had a couple typo's in the shortened version of the script.
still need to know how to get PriceHandler1 to work with a dynamic document object.

=======================================
<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>
<SCRIPT Language=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
function PriceHandler1(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = "document.form.Fee" + CostFactor + ".value";
Apt = Fee;
var ThsMsg = CostFactor + " * " + Price + " = " + Fee +"\n" + Apt;
alert(ThsMsg);
}
function PriceHandler2(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = "document.form.Fee" + CostFactor + ".value";
document.form.Fee2.value = Fee;
var ThsMsg = CostFactor + " * " + Price + " = " + Fee;
alert(ThsMsg);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="" METHOD=POST NAME="form">
Line 1 <INPUT TYPE=TEXT NAME=PriceChange1 Value="150" onChange="PriceHandler2('1', document.form.PriceChange1.value)"> <INPUT TYPE=TEXT NAME=Fee1 VALUE="150"><BR>
Line 2 <INPUT TYPE=TEXT NAME=PriceChange2 Value="150" onChange="PriceHandler2('2', document.form.PriceChange2.value)"> <INPUT TYPE=TEXT NAME=Fee2 VALUE="300"><BR>
Line 3 <INPUT TYPE=TEXT NAME=PriceChange3 Value="150" onChange="PriceHandler2('3', document.form.PriceChange3.value)"> <INPUT TYPE=TEXT NAME=Fee3 VALUE="450"><BR>
Line 4 <INPUT TYPE=TEXT NAME=PriceChange4 Value="150" onChange="PriceHandler2('4', document.form.PriceChange4.value)"> <INPUT TYPE=TEXT NAME=Fee4 VALUE="600"><BR>
<INPUT TYPE=SUBMIT>
</FORM>
 
I found my answer...
thanks anyway...

============ ANSWER ==================
<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>
<SCRIPT Language=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
function PriceHandler1(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = eval("document.form.Fee" + CostFactor);
Apt.value = Fee;
var ThsMsg = CostFactor + " * " + Price + " = " + Fee +"\n" + Apt;
alert(ThsMsg);
}
function PriceHandler2(CostFactor,Price)
{
var Fee = CostFactor * Price;
var Apt = "document.form.Fee" + CostFactor + ".value";
var ThsMsg = CostFactor + " * " + Price + " = " + Fee;
alert(ThsMsg);
document.form.Fee2.value = Fee;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="" METHOD=POST NAME="form">
Line 1 <INPUT TYPE=TEXT NAME=PriceChange1 Value="150" onChange="PriceHandler1('1', document.form.PriceChange1.value)"> <INPUT TYPE=TEXT NAME=Fee1 VALUE="150"><BR>
Line 2 <INPUT TYPE=TEXT NAME=PriceChange2 Value="150" onChange="PriceHandler1('2', document.form.PriceChange2.value)"> <INPUT TYPE=TEXT NAME=Fee2 VALUE="300"><BR>
Line 3 <INPUT TYPE=TEXT NAME=PriceChange3 Value="150" onChange="PriceHandler1('3', document.form.PriceChange3.value)"> <INPUT TYPE=TEXT NAME=Fee3 VALUE="450"><BR>
Line 4 <INPUT TYPE=TEXT NAME=PriceChange4 Value="150" onChange="PriceHandler1('4', document.form.PriceChange4.value)"> <INPUT TYPE=TEXT NAME=Fee4 VALUE="600"><BR>
<INPUT TYPE=SUBMIT>
</FORM>
 
I strongly urge you to use other means than the "eval" statement. here's the suggested method:

Code:
var Apt = document.forms['YourFormName'].elements['Fee' + CostFactor];



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top