Reputation: 23
I have a databound Checkboxlist in asp.NET with DataTextField and DataValueField.
Now if i select a single item in that list, i need to have the value of only that item.
I found lots of solutions to find the values of all selected items but that is not what i need. The only value i need is the one i justed selected or deselected.
Is there a way to get this value with jQuery code? For example in an alert like in the code below.
$("#checkboxlist").click(function () {
alert('value of the item that just got (de)selected');
});
Thanks alot!
UPDATE: This is the html part:
<span id="checkboxlist">
<input id="checkboxlist_0" type="checkbox" name="checkboxlist$checkboxlist_0" value="11" />
<label for="checkboxlist_0">Item 1</label><br />
<input id="checkboxlist_1" type="checkbox" name="checkboxlist$checkboxlist_1" value="12" />
<label for="checkboxlist_1">Item 2</label><br />
<input id="checkboxlist_2" type="checkbox" name="checkboxlist$checkboxlist_2" value="13" />
<label for="checkboxlist_2">Item 3</label><br />
<input id="checkboxlist_3" type="checkbox" name="checkboxlist$checkboxlist_3" value="14" />
<label for="checkboxlist_3">Item 4</label><br />
</span>
Upvotes: 2
Views: 1540
Reputation: 12815
This code should work (Attribute Ends with selector):
$('[id$="checkboxlist"] input').click(function () {
alert($(this).val());
});
asp:CheckBoxList
will be rendered as some element (in your case it should be span
) with ID like ctl00_contenttop_checkboxlist
and inputs inside it. Code above will find all inputs inside element with ID that ends with checkboxlist
and will be fired when one of those elements is clicked.
But attribute ends with could be slow, so it could be better to do something like this:
$("#<%=checkboxlist.ClientID %> input").click(function () {
alert($(this).val());
});
checkboxlist.ClientID
will return ID of wrapper element on client side.
UPD:
According to your HTML, it looks like ClientID is not needed (as it looks like you are using .net 4 with ClientIDMode
enabled). In that case you can simply use:
$("#checkboxlist input").click(function () {
alert($(this).val());
});
Upvotes: 3