Get the Values of CheckBoxList in Comma Separated form using jQuery

Get the values of CheckBoxList in comma separated form using JQuery

Server side code

Suppose you have already fetched data from the database, then bind the CheckBoxList1 as

     CheckBoxList1.DataSource = dataSet.Tables[0];
     CheckBoxList1.DataBind();

Below code is used to set the values of CheckBoxList1 check box list for JQuery functionality

     foreach (ListItem li in CheckBoxList1.Items)
           li.Attributes.Add("'empIdValue'", li.Value);

Client side code

       var valuesEmpId = "";
       $j("#<%=CheckBoxList1.ClientID %> input[type=checkbox]:checked").each(function() {
                  varEmpId = $j(this).parent().attr('empIdValue');
                        valuesEmpId += varEmpId + ",";
        });

valuesEmpId will store data as 1,2,3,4,5,6,

To remove last comma from the above string

        if (valuesEmpId != "") {
              valuesEmpId = valuesEmpId.substring(0, valuesEmpId.length - 1);
        }

Output:

valuesEmpId = 1,2,3,4,5,6