Obtain Dynamically Created TextBox Name

C

crjunk

I have a web page that displays multiple records for a company. The
input text boxes that display my data are created dynamically. I'm
creating these input boxes dynamically because the number of records
for each company varies. The name of each input box increments by one
for each record (Ex: A1, B1, C1 | A2, B2, C2, etc..)

On my web page the user will enter in a number into the
txtGrossPayroll(some number) input box. I have added an
OnKeyDown="ComputePremium()" and an OnKeyUp="ComputePremium()" inside
my input boxes. When the user begins to type in a number the following
script runs and displays the premium inside the txtPremium(some number)
input box.


<script type="text/javascript" language="JavaScript">
<!--
function ComputePremium() {
document.ScheduleForm.txtPremium1.value =
Math.round((document.ScheduleForm.txtRate1.value *
(document.ScheduleForm.txtGrossPayroll1.value / 100)), 0);
}
// -->
</script>

Currently, I have the script so that it will only run for my 1st row of
records. Can the script be modified so that it is intelligent enough
to know the correct input box name to use? If so how?

I'm not very familiar with JavaScripts; sorry if this is something that
is very easy to do.

Thanks!
CR Junk
 
R

Richard Cornford

... . The input text boxes that display my data are created
dynamically.

Client-side or server-side?
I'm creating these input boxes dynamically because the number
of records for each company varies. The name of each input
box increments by one for each record (Ex: A1, B1, C1 | A2,
B2, C2, etc..)

On my web page the user will enter in a number into the
txtGrossPayroll(some number) input box. I have added an
OnKeyDown="ComputePremium()" and an OnKeyUp="ComputePremium()"

Two calculations per key press?
inside
my input boxes. When the user begins to type in a number the
following script runs and displays the premium inside the
txtPremium(some number) input box.


<script type="text/javascript" language="JavaScript">
<!--

This "hide scripts form older browsers" stuff is superfluous.
function ComputePremium() {
document.ScheduleForm.txtPremium1.value =
Math.round((document.ScheduleForm.txtRate1.value *
(document.ScheduleForm.txtGrossPayroll1.value / 100)), 0);
^
What is the zero for?
}
// -->
</script>

Currently, I have the script so that it will only run for
my 1st row of records.

Because you have hard-coded the form control name.
Can the script be modified so that it is intelligent
enough to know the correct input box name to use?
Yes.

If so how?

By providing the row information as a parameter to the function call, in
some form, and then using bracket notation to reference the form
controls in the correct row.
I'm not very familiar with JavaScripts; sorry if this
is something that is very easy to do.

It is very easy to do, but how it is specifically done depends on the
HTML being scripted.

Richard.
 
C

crjunk

Richard said:
Client-side or server-side?

The input boxes are being created on the server-side.
Two calculations per key press?

I didn't think that that I needed to have OnKeyUp and OnKeyDown, but a
script that I used as a reference used both events. I went with how
they had created their script. Will either one of these accomplish
what I want?
What is the zero for?

Sorry about that. This is old code that I forgot to remove from the
desktop app that I'm converting and moving online.
By providing the row information as a parameter to the function call, in
some form, and then using bracket notation to reference the form
controls in the correct row.

Since I'm creating each input box on the server-side, I now see how I
can pass the row number to the function when the server is generating
the HTML. Can you provide an example of what the syntax looks like for
using bracket notation when referencing the form controls in the
formula?


Thanks for your help Richard!

CR Junk
 
C

crjunk

Hi Richard,

I continued playing around with my function and came up with the
following:

function ComputePremium(i) {
document.ScheduleForm["txtPremium" + i].value =
Math.round((document.ScheduleForm["txtRate" + i].value *
(document.ScheduleForm["txtGrossPayroll" + i].value / 100)));
}

So far, it seems to be working correctly. Is this what you were
refering to when you mentioned bracket notation.

Thanks,

CR Junk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top