Allows you to see if different ListItems have their own text and values? -
I have explained all possible cases of getting values ββ/ text of a checkbox or checkboxlist, as shown below: -
Case-1: - If the elements and values ββof the CheckBoxList element are the same
<asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="180"> <asp:ListItem Value="SQL" Text="">SQL</asp:ListItem> <asp:ListItem Value="ASP.Net" Text="">ASP.Net</asp:ListItem> <asp:ListItem Value="jQuery" Text="">jQuery</asp:ListItem> <asp:ListItem Value="MVC" Text="">MVC</asp:ListItem> <asp:ListItem Value="IIS" Text="">IIS</asp:ListItem> </asp:CheckBoxList>
call the following js onclick function or where you need it (if you need a call in the document.ready block)
function getChkVal1() { $chk_values = $("[id$=CheckBoxList1] input[type=checkbox]:checked").map(function (index, foElem) { return $(this).next().html(); }).get(); alert($chk_values); }
Case-2: - If the values ββof the CheckBoxList elements are specified in numerical order (for example, 1,2,3 ... n, of course, we often use this path)
<asp:CheckBoxList ID="CheckBoxList2" runat="server" BackColor="#ffcccc" Width="180"> <asp:ListItem Value="1" Text="">SQL</asp:ListItem> <asp:ListItem Value="2" Text="">ASP.Net</asp:ListItem> <asp:ListItem Value="3" Text="">jQuery</asp:ListItem> <asp:ListItem Value="4" Text="">MVC</asp:ListItem> <asp:ListItem Value="5" Text="">IIS</asp:ListItem> </asp:CheckBoxList>
Function
js will be lower
function getChkVal2() { $chk_values = $("[id$=CheckBoxList2] input[type=checkbox]").map(function (index, foElem) { if ($(this).is(":checked")) return (index + 1); }).get(); alert($chk_values); }
Case-3: - If the values ββof the CheckBoxList element have some short forms / initials of its text (for example, for Mango I would say "M")
Here I used a trick to get the checkbox values ββon the page so that it is easily accessible when writing a js script. That is, I use a hidden field with checkboxlist, which will receive all available values ββin the checkboxlist during parsing html and store them on the page in its value. See below: -
<asp:CheckBoxList ID="CheckBoxList3" runat="server" BackColor="#ffccff" Width="180"> <asp:ListItem Value="S" Text="">SQL</asp:ListItem> <asp:ListItem Value="A" Text="">ASP.Net</asp:ListItem> <asp:ListItem Value="J" Text="">jQuery</asp:ListItem> <asp:ListItem Value="M" Text="">MVC</asp:ListItem> <asp:ListItem Value="I" Text="">IIS</asp:ListItem> </asp:CheckBoxList> <input type="hidden" id="hdnChkBox3" value='<%=String.Join( ",", CheckBoxList3.Items.Cast<ListItem>().Select(item=>item.Value) ) %>' />
I use html hiddenfield, not runat = server, since we do not need to write any code in cs using this hidden field. Here, the role of the hidden field is nothing more than an assistant that will provide comfort when writing js and getting flag values.
And now let's see what the script will look like: -
function getChkVal3() { $chk_items = $("#hdnChkBox3").val().split(","); // Get all checkboclist values from hiddenfield and convert it to array using split() // so now we have ItemIndex and Values Index in Array as parallel $chk_values = $("[id$=CheckBoxList3] input[type=checkbox]").map(function (index, foElem) { if ($(this).is(":checked")) { return ($chk_items[index]); } }).get(); alert($chk_values); }
Similarly, you can do on radibuttonlist / radiobutton.