Obtain Dynamically Created TextBox Name

Discussion in 'Javascript' started by crjunk@earthlink.net, Jun 24, 2005.

  1. Guest

    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
     
    , Jun 24, 2005
    #1
    1. Advertising

  2. wrote:
    > ... . 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.
     
    Richard Cornford, Jun 25, 2005
    #2
    1. Advertising

  3. crjunk Guest

    Richard Cornford wrote:

    > 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
     
    crjunk, Jun 27, 2005
    #3
  4. crjunk Guest

    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
     
    crjunk, Jun 27, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Johnston
    Replies:
    5
    Views:
    572
    Natty Gur
    Jul 28, 2003
  2. Shilpi Chaudhry
    Replies:
    1
    Views:
    3,064
    Hermit Dave
    Sep 15, 2004
  3. Raed Sawalha
    Replies:
    2
    Views:
    1,536
    =?Utf-8?B?UHN5Y2hv?=
    Oct 5, 2004
  4. Jeff Kish
    Replies:
    4
    Views:
    871
    Martin Honnen
    Oct 30, 2008
  5. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
Loading...

Share This Page