How can I change the Text for a CheckBox without a postback?
<asp:CheckBox ID="CheckBox1" runat="server" Text="Open" />
I would like to toggle the Text to read "Open" or "Closed" when the CheckBox is clicked on the client.
function changeCheckboxText(checkbox)
{
if (checkbox.checked)
checkbox.nextSibling.innerHTML = 'on text';
else
checkbox.nextSibling.innerHTML = 'off text';
}
called as:
<asp:CheckBox runat="server" ID="chkTest" onclick="changeCheckboxText(this);" />
Just FYI, it's usually bad practice to change the text of the checkbox label because it tends to confuse the user.
If you're interested to use a framework javascript like jQuery, i propose a solution ilke this:
$("input[id$=CheckBox1]").click(function() {
if ($(this).attr("checked")) {
$(this).next("label:first").text("Open");
}
else {
$(this).next("label:first").text("Close");
}
});
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