Access dynamically created Form elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will

    #1

    Access dynamically created Form elements

    Hi, I have a problem trying to validate dynamically created html form
    elements using javascript. I have dynamically created a check box
    using ASP for each record in a recordset and have given each a unique
    ID using the primary key from a db table. e.g "chk" + "1" for record 1
    and "chk" + "2" for record 2 etc. This means each box is called chk1
    and chk2 respectively. This works fine and changes dependant on the
    recordset used and allows me to pass details to another page for
    processing.

    However I want to validate each checkbox to check if it is clicked or
    not when a submit button is pressed. I cannot work out how to refer to
    the elements in the javascript as they have diferent ids every time
    the page is created. If I pass in the ID of the check box to the
    javascript function is there a way I can concatenate the id to the
    prefix "chk" and refer to the element in the form. This may sound
    confusing it is to me! Basically every check box on the page is called
    "chk" then is suffixed by the id of the record. Now in the javascript
    I wish to refer to each checkbox in this sort of manner

    document.sendfo rm."chk" + recordID

    I have tried this and it didnt seem to work so if anyone has any ideas
    it would be much appreciated.

    Cheers
    Will
  • lallous

    #2
    Re: Access dynamically created Form elements

    Hello Will,

    You can access as:

    theForm.element s['chk'+id].Method/Property

    You can also walk in the elements list as:
    for (i=0;i<theForm. elements.length ;i++)
    {
    e = theForm.element s[i];
    if (e.name.substr( 0, 3) == 'chk')
    // do something here
    }


    --
    Elias
    "Will" <william@ntsols .com> wrote in message
    news:8a45f8d9.0 401230411.7fb0f 900@posting.goo gle.com...[color=blue]
    > Hi, I have a problem trying to validate dynamically created html form
    > elements using javascript. I have dynamically created a check box
    > using ASP for each record in a recordset and have given each a unique
    > ID using the primary key from a db table. e.g "chk" + "1" for record 1
    > and "chk" + "2" for record 2 etc. This means each box is called chk1
    > and chk2 respectively. This works fine and changes dependant on the
    > recordset used and allows me to pass details to another page for
    > processing.
    >
    > However I want to validate each checkbox to check if it is clicked or
    > not when a submit button is pressed. I cannot work out how to refer to
    > the elements in the javascript as they have diferent ids every time
    > the page is created. If I pass in the ID of the check box to the
    > javascript function is there a way I can concatenate the id to the
    > prefix "chk" and refer to the element in the form. This may sound
    > confusing it is to me! Basically every check box on the page is called
    > "chk" then is suffixed by the id of the record. Now in the javascript
    > I wish to refer to each checkbox in this sort of manner
    >
    > document.sendfo rm."chk" + recordID
    >
    > I have tried this and it didnt seem to work so if anyone has any ideas
    > it would be much appreciated.
    >
    > Cheers
    > Will[/color]


    Comment

    Working...