rysahara
rysahara

Reputation: 356

Populating checkboxes

How do I add items in checkboxes using jQuery?
To mount a DropDown list use the following:

$.each(dataResult, function (index, itemData) {
    select.append($('<option/>', {
        value: itemData.Value,
        text: itemData.Text
    }));
});

I need to know to build a checkboxes?

Upvotes: 1

Views: 5730

Answers (1)

Sampath
Sampath

Reputation: 65860

Please try is as below.

<script type="text/javascript" language="javascript">
        function PopulateCheckBoxList() {
            $.ajax({
                type: "POST",
                url: "CheckBoxList/GetCheckBoxDetails",
                contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });
        }
        function AjaxSucceeded(result) {
            BindCheckBoxList(result);
        }
        function AjaxFailed(result) {
            alert('Failed to load checkbox list');
        }
        function BindCheckBoxList(result) {

            var items = JSON.parse(result.d);
            CreateCheckBoxList(items);
        }
        function CreateCheckBoxList(checkboxlistItems) {
            var table = $('<table></table>');
            var counter = 0;
            $(checkboxlistItems).each(function () {
                table.append($('<tr></tr>').append($('<td></td>').append($('<input>').attr({
                    type: 'checkbox', name: 'chklistitem', value: this.Value, id: 'chklistitem' + counter
                })).append(
                $('<label>').attr({
                    for: 'chklistitem' + counter++
                }).text(this.Name))));
            });

            $('#dvCheckBoxListControl').append(table);
        }
    </script>

Please check below mentioned link for more info.

Note : This is for web form.But you can easily use it with MVC also.

Bind a CheckBox list from database using jQuery AJAX

Upvotes: 2

Related Questions