jQuery pre-fill checkboxes

by ParanoidAndroid77

JavaScript

//if in editing mode, do not allow changes either if the field is a primary key or it triggers a jump
if (window.localStorage.edit_mode && (input.is_primary_key === '1' || input.has_jump === '1')) {
    DISABLED = 'disabled="disabled"';
}

//display all checkboxes options
$(input.options).each(function (index) {

    var name = "choice";
    var option_value = this.value.trim();
    var option_label = this.label.trim();
    var option_id = 'checkbox-choice-' + (index + 1);
    var i;
    var iLength = value.length;

    //check if we have any value stored. For checkboxes, 'value' will be an array
    for (i = 0; i < iLength; i++) {

        CHECKBOX_CHECKED = "";

        //if any match is found, pre-select that checkbox in the markup
        if (value[i].trim() === option_label) {
            CHECKBOX_CHECKED = 'checked="checked"';
            break;
        }
    }

    HTML += '<label>';
    HTML += '<input type="checkbox" ' + CHECKBOX_CHECKED + ' ' + DISABLED + ' name="' + name;
    HTML += '" id="' + option_id;
    HTML += '" value="' + option_value;
    HTML += '" data-label="' + option_label;
    HTML += '" />' + option_label;
    HTML += '</label>';

});

span_label.append(HTML);
$('div#input-checkbox').trigger("create");