I want to get the text of the asp:CheckBox using JavaScript.
<asp:CheckBox ID="chkRoles" runat="server" Text="Testing" onclick="javascript:test(this);" />
How to retrieve the text Testing from the checkbox? I have tried the following,
JavaScript
function test(obj)
{
var text = obj.value;
}
You can use checkbox.nextSibling.innerHTML in javascript as shown below :
<script type="text/javascript">
function changeCheckboxText(checkbox) {
if (checkbox.checked) {
alert(checkbox.nextSibling.innerHTML);
}
}
</script>
The nextSibling property returns the node immediately following the specified node, in the same tree level.
Code :
CheckBox chk = new CheckBox();
chk.ID = "chkRoles";
chk.Text = "Check";
PlaceHolder1.Controls.Add(chk);
chk.Attributes.Add("onclick", "changeCheckboxText(this)");
Also using jQuery :
$("input:checkbox").click(function() {
var $label = $(this).next('label');
alert($label.text());
});
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With