Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Text from asp:CheckBox

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;
}
like image 977
Jesuraja Avatar asked Dec 02 '25 11:12

Jesuraja


1 Answers

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

like image 148
Tharif Avatar answered Dec 05 '25 02:12

Tharif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!