Combo check box, user must check max 3 options

I want the user to be able to select max 3 options on my combo check box. If he has 3 he wont be able to check a 4 one unless he unchecks first one. I tried this code but with no luck.

combo.attachEvent(“onBeforeCheck”, function (value, newState) {
var checked_arr = combo.getChecked();
var arr_text = new Array();
var count = 0;
for (var i = 0; i < checked_arr.length; i++) {
arr_text.push(combo.getOption(checked_arr[i]).text);
count++;
}
if(count>=3){
return false;
}else
return true;

});

Your code works well for me locally, But I’d suggest you to add the condition to check the nes state of a checkbox to prevent blocking the checkbox unchecking:

myCombo.attachEvent("onBeforeCheck", function (value, newState) {
var checked_arr = myCombo.getChecked();
var arr_text = new Array();
var count = 0;
for (var i = 0; i < checked_arr.length; i++) {
arr_text.push(myCombo.getOption(checked_arr[i]).text);
count++;
}
if(count>=3 && newState==true){ //check the newState
return false;
}else
return true;

});

http://snippet.dhtmlx.com/f26377cea

1 Like

thanks a lot, it works!