Obtain Dynamically Created TextBox Name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crjunk@earthlink.net

    #1

    Obtain Dynamically Created TextBox Name

    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="Comp utePremium()" and an OnKeyUp="Comput ePremium()" 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="JavaS cript">
    <!--
    function ComputePremium( ) {
    document.Schedu leForm.txtPremi um1.value =
    Math.round((doc ument.ScheduleF orm.txtRate1.va lue *
    (document.Sched uleForm.txtGros sPayroll1.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

  • Richard Cornford

    #2
    Re: Obtain Dynamically Created TextBox Name

    crjunk@earthlin k.net wrote:[color=blue]
    > ... . The input text boxes that display my data are created
    > dynamically.[/color]

    Client-side or server-side?
    [color=blue]
    > 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="Comp utePremium()" and an OnKeyUp="Comput ePremium()"[/color]

    Two calculations per key press?
    [color=blue]
    > 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="JavaS cript">
    > <!--[/color]

    This "hide scripts form older browsers" stuff is superfluous.
    [color=blue]
    > function ComputePremium( ) {
    > document.Schedu leForm.txtPremi um1.value =
    > Math.round((doc ument.ScheduleF orm.txtRate1.va lue *
    > (document.Sched uleForm.txtGros sPayroll1.value / 100)), 0);[/color]
    ^
    What is the zero for?
    [color=blue]
    > }
    > // -->
    > </script>
    >
    > Currently, I have the script so that it will only run for
    > my 1st row of records.[/color]

    Because you have hard-coded the form control name.
    [color=blue]
    > Can the script be modified so that it is intelligent
    > enough to know the correct input box name to use?[/color]

    Yes.
    [color=blue]
    > If so how?[/color]

    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.
    [color=blue]
    > I'm not very familiar with JavaScripts; sorry if this
    > is something that is very easy to do.[/color]

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

    Richard.


    Comment

    • crjunk

      #3
      Re: Obtain Dynamically Created TextBox Name

      Richard Cornford wrote:
      [color=blue]
      > Client-side or server-side?[/color]

      The input boxes are being created on the server-side.
      [color=blue]
      > Two calculations per key press?[/color]

      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?
      [color=blue]
      > What is the zero for?[/color]

      Sorry about that. This is old code that I forgot to remove from the
      desktop app that I'm converting and moving online.
      [color=blue]
      > By providing the row information as a parameter to the function call, in[/color]
      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

      Comment

      • crjunk

        #4
        Re: Obtain Dynamically Created TextBox Name

        Hi Richard,

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

        function ComputePremium( i) {
        document.Schedu leForm["txtPremium " + i].value =
        Math.round((doc ument.ScheduleF orm["txtRate" + i].value *
        (document.Sched uleForm["txtGrossPayrol l" + 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

        Comment

        Working...