Access dynamically created Form elements

W

Will

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
 
L

lallous

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
}
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top