How to 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;     
       }     
    }     
}

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.