Access dynamically created Form elements

Discussion in 'Javascript' started by Will, Jan 23, 2004.

  1. Will

    Will Guest

    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.sendform."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
     
    Will, Jan 23, 2004
    #1
    1. Advertising

  2. Will

    lallous Guest

    Hello Will,

    You can access as:

    theForm.elements['chk'+id].Method/Property

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


    --
    Elias
    "Will" <> wrote in message
    news:...
    > 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.sendform."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, Jan 23, 2004
    #2
    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. Ittay Dror
    Replies:
    0
    Views:
    382
    Ittay Dror
    Apr 19, 2004
  2. msimmons
    Replies:
    0
    Views:
    544
    msimmons
    Jul 16, 2009
  3. Nicholas Couch

    IE6 won't hide dynamically created span elements

    Nicholas Couch, Sep 17, 2004, in forum: Javascript
    Replies:
    20
    Views:
    240
    Thomas 'PointedEars' Lahn
    Sep 26, 2004
  4. Robert Oschler
    Replies:
    1
    Views:
    140
    Robert Oschler
    Sep 3, 2005
  5. VK
    Replies:
    8
    Views:
    178
Loading...

Share This Page