Change asp: CheckBox text when clicked

How can I change text for CheckBox without postback?

<asp:CheckBox ID="CheckBox1" runat="server" Text="Open" /> 

I would like to switch the text to read β€œOpen” or β€œClosed” when a click on a click is clicked on a client.

+4
source share
3 answers
 function changeCheckboxText(checkbox) { if (checkbox.checked) checkbox.nextSibling.innerHTML = 'on text'; else checkbox.nextSibling.innerHTML = 'off text'; } 

called:

 <asp:CheckBox runat="server" ID="chkTest" onclick="changeCheckboxText(this);" /> 

Just FYI, usually the wrong practice is to change the text of the flag label as it tends to confuse the user.

+9
source

If you are interested in using a javascript framework like jQuery, I suggest ilke this solution:

 $("input[id$=CheckBox1]").click(function() { if ($(this).attr("checked")) { $(this).next("label:first").text("Open"); } else { $(this).next("label:first").text("Close"); } }); 
+2
source

3 Simple Options

Use JavaScript to change the side of the text client. By registering the event in the .CS class as follows:

 CheckBox1.Attributes.Add("JavaScipt") 

Use HTML checkbox with jQuery

Or add Ajax Update Panel

Some AJax starter videos

+1
source

All Articles