PDA

View Full Version : How to get all the elements in a form in a function


seema
11-17-2005, 02:02 PM
Hello,

I am working on a html page which has many checkboxes. The checkbox names are composed at run time. When the user selects one checkbox I need to disable or enable the rest of them.

I wrote a function to handle "Onclick" event for the checkbox. However in that function I need to get the list of all the checkboxes on that form. I tried many methods and they didn't quite work well. Anyone have a suggestion ?

Thanks.

RysChwith
11-17-2005, 04:25 PM
var boxes = new Array();
var forms = document.getElementsByTagName( "form" );
var form = forms[ 0 ]; //or whichever form you want
var inputs = form.getElementsByTagName( "input" );

for( var i = 0; i < inputs.length; i++ ) {
if( inputs[ i ].type == "checkbox" ) {
boxes[ boxes.length ] = inputs[ i ];
}
}Once that's done, boxes[] will contain references to all the checkboxes in the form.

Rys