Limit the number of checkboxes in a multiple response question

I am trying to limit the number of checkboxes to 3 for a response to a select questions with total 5 responses. Using the following code but it seems to not working for me. Would you please point me where I'm getting this wrong?

<script>
function SSI_CustomGraphicalCheckbox(graphical, input, bln) {
// Settings
var maximum = 3;

// Run
if (bln) {
var match = input.name.match(/^[% QuestionName() %]_(\d+)$/);
if (match) {
var checks = 0;
$('input[name="hid_list_[% QuestionName() %]"]').val().split(',').forEach(function(item){
if (SSI_GetValue('[% QuestionName() %]_' + item)) {
checks++;
}
});
if (checks > maximum) {
SSI_SetSelect('[% QuestionName() %]_' + match[1], false);
}
}
}
}
</script>

5 replies