faqts : Computers : Programming : Languages : JavaScript : Forms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

24 of 66 people (36%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can i get an array that contains all checkboxes in document and submit them only if they are che

May 12th, 2004 02:23
Suban Asif, selene rebane,


By default, only those checkboxes are submitted which are checked.
Neverthless, below is code for the first part of your question:
var arr = new Array();
for ( var i = 0 ; i < document.myForm.elements.length; i++ ){
  var elm = document.myForm.elements[i];
  if ( elm.type == "checkbox" ){
    arr[arr.length] = elm;
  }
}
For the second part,
var toSubmit = true;
for( var i = 0; i < arr.length; i++){
 var checkbox = arr[i];
 if ( !checkbox.checked ) {
    toSubmit = false;
    break;
 }
}
if ( toSubmit ) {
 document.myForm.submit();
}