Select All CheckBox or Element in a Form HTML

Creating a form dynamically at run time – this also means we should give unique names to each html controls. For example we have a datagrid like object and its first column is checkbox, how should we check all of the rows.

Here is my code that iterates to all controls in an html document and check if it is CheckBox type:

//flag can have the value of 1 = checked, else unchecked
function checkAll(flag) {    
  var f = document.forms["yourformname"], i;     
    for(i = 0; i < f.length; i++){ //iterate to each control    
      if(f[i].type == 'checkbox'){ //if type checkbox    
        if(flag == 1)     
          f[i].checked = true;     
        else     
          f[i].checked = false;     
       }     
    }     
}
Select All CheckBox or Element in a Form HTML Select All CheckBox or Element in a Form HTML Reviewed by czetsuya on Thursday, October 08, 2009 Rating: 5

No comments:

Powered by Blogger.