Checkbox selection logic
November 30, 2008
This js restricts the selection from a group of checkboxes to four. If a fifth one is selected , then the first one whichever was selected will be unchecked.
A combination of js and jQuery.
// Restricting the vehicle selection to FOUR – Start
var vehList=new Array();
vehList[0]=””;
vehList[1]=””;
vehList[2]=””;
vehList[3]=””;
var vehObjList = new Array();
vehObjList[0]=””;
vehObjList[1]=””;
vehObjList[2]=””;
vehObjList[3]=””;
function restrictVeh(obj,name){
var selVin = name;
var remVeh = “”;
var remObj;
if($(obj).attr(’checked’)){
for(var incr=0;incr <= 3 ; incr++){
if(vehList[incr]==””){
vehList[incr]=selVin;
vehObjList[incr]=obj;
break;
}
if(incr==3){
remVeh = vehList[0];
remObj = vehObjList[0];
$(remObj).attr(’checked’,false);
for(var cnt=0;cnt<3;cnt++){
vehList[cnt]=vehList[cnt+1];
vehObjList[cnt]= vehObjList[cnt+1];
}
vehList[3]=selVin;
vehObjList[3]=obj;
}
}
}else{
for(var incr=0;incr <= 3 ; incr++){
if(vehList[incr]==selVin){
vehList[incr]=””;
vehObjList[incr]=””;
for(var cnt=incr;cnt<3;cnt++){
vehList[cnt]=vehList[cnt+1];
vehObjList[cnt]=vehObjList[cnt+1]
}
vehList[3]=””;
vehObjList[3]=””;
}
}
}
/* for(var incr=0;incr <= 3 ; incr++){
alert(vehList[incr]);
} */
}
// Restricting the vehicle selection to FOUR – End
Entry Filed under: tips and tricks. Tags: checkbox, javascript, jQuery, js.
Trackback this post | Subscribe to the comments via RSS Feed