I am using CheckBoxList control of Asp.Net. In order to get selected checkboxes i am adding a custom attribute for Value after binding the data to the checkboxlist.
I am able to get the checked checkboxes in jQuery but i dont know how to find a particular checkbox using that custom attribute in the checkboxlist.
here is the code:
After databind in .cs file:
foreach (ListItem li in cblRequestTypes.Items)
li.Attributes.Add("itemValue", li.Value);
To get the selected checboxes:
$(":checkbox").each(function() {
if (this.checked) {
selectedChecks += $(this).parent().attr("itemValue") + "~";
}
});
Now i am passing the values in the querystring and based on that i have to find the checkbox which has the itemValue attribute sent in the querystring. This part is not working or may be i am missing something here.
var id = $.QueryString["id"]
$(id.split("~")).each(function (i, item) {
$("checkbox[itemValue=" + item +"]").attr('check',checked');
});
This is how the HTML for CheckBoxList is rendered:
<span itemValue="3"><input id="chkBoxList_0" type="checkbox" name="chkBoxList$0" /><label for="chkBoxList_0">Text 1</label></span>
<span itemValue="5"><input id="chkBoxList_1" type="checkbox" name="chkBoxList$1" /><label for="chkBoxList_1">Text 2</label></span>
<span itemValue="6"><input id="chkBoxList_2" type="checkbox" name="chkBoxList$2" /><label for="chkBoxList_2">Text 3</label></span>
<span itemValue="7"><input id="chkBoxList_3" type="checkbox" name="chkBoxList$3" /><label for="chkBoxList_3">Text 4</label></span>
<span itemValue="8"><input id="chkBoxList_4" type="checkbox" name="chkBoxList$4" /><label for="chkBoxList_4">Text 5</label></span>
<span itemValue="9"><input id="chkBoxList_5" type="checkbox" name="chkBoxList$5" /><label for="chkBoxList_5">Text 6</label></span>
~ever again. I've already cleaned up one ASP project that had that in it.valueattribute that any input element already has? Or put theitemValueon the checkbox itself? Or, if you're using HTML5, use adata-item-valueattribute - then your markup is still valid