Skip to content Skip to sidebar Skip to footer

Dynamically Limit Maximum Number Of Checkboxes Selectable

I found many scripts to limit max number of checkboxes that can be selected in a page with javascript, for example here is one: http://www.plus2net.com/javascript_tutorial/checkbox

Solution 1:

To alter that script to be a percentage vs. a fixed total you just need to take 30% of the total number of elements.

function chkcontrol(j) {
  var length = document.form1.chb.length;

  // Max number allowed checked is 30% of the total
  var max = Math.round(length * .3);
  var total = 0;
  for(var i = 0; i < length; i++){
    if(document.form1.ckb[i].checked){
      total = total + 1;
    }

    if(total > max){
      alert("Please Select only three") 
      document.form1.ckb[j].checked = false ;
      return false;
    }
  }
}

Post a Comment for "Dynamically Limit Maximum Number Of Checkboxes Selectable"