views:

47

answers:

2

I want to add a value to a checkboxlist using javascript /jquery.The code below is my sample code

function getExpertise() {

          $.ajax({

           type: "POST",

           url: "Sample.asmx/GetExpertiseBySpecialization",

           data: "{sId: '" + $('#<%=ddlSpecialization.ClientID%>').val() + "'}",

                 contentType: "application/json; charset=utf-8",

                 dataType: "json",

                 success: function(response) {

         var expertise = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

        $('#<%=chkExpertise.ClientID%>').attr('disabled', false).removeOption(/./).addOption('-1', 'Please select expertise');


        for (var i = 0; i < expertise.length; i++) {

            var val = expertise[i].Id;

            var text = expertise[i].Expertise;

            $('#<%=chkExpertise.ClientID%>').addOption(val, text, false);

                                           }

                                       }

                                   });

                               }
A: 
$('#checkbox_id').attr('checked', true)
NM
A: 

source :

http://forums.asp.net/p/1416683/3127300.aspx

A CheckBoxList (or RadioButtonList for that matter) renders as a tag with CheckBoxes and an HTML Label element in the tags. To add items you would need to add a and or to the table, which I definitely would not advise you to do with JavaScript, since they would not persist server-side and would disappear if a PostBack occurred. I'd suggest you do a PostBack and add the items server-side.

<asp:CheckBoxList id="CheckBoxList1" runat="server">
 <asp:listitem Value="1">Item 1</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="addToCheckBoxListControl('Item 2', '2');" value="Add To CheckBoxList" />

<script type="text/javascript">
<!--
function addToCheckBoxListControl(textValue, valueValue)
{
 var tableRef = document.getElementById('<%= CheckBoxList1.ClientID %>');

 var tableRow = tableRef.insertRow();
 var tableCell = tableRow.insertCell();

 var checkBoxRef = document.createElement('input');
 var labelRef = document.createElement('label');

 checkBoxRef.type = 'checkbox';
 labelRef.innerHTML = textValue;
 checkBoxRef.value = valueValue;

 tableCell.appendChild(checkBoxRef);
 tableCell.appendChild(labelRef);
}
// -->
</script>
Haim Evgi
this code works, the problem is, it should consist a default item (eg <asp:listitem Value="1">Item 1</asp:listitem> ). Now how can I removed this default data using javascript.I want to removed this if I added already an item using javascript