I have a asp:checkbox and a asp:textbox on my aspx page , I want to know show the textbox when the checkbox is checked and hide it if the checkbox is unckecked using javascript , I really apprecieatee anyone who could help me .
3 Answers
you can uee this code
<script type="text/javascript">
function radio_yes(){
if(document.getElementById("c1").checked==true)
document.getElementById("atext").style.visibility="visible";
else
document.getElementById("atext").style.visibility="hidden";}
</script>
<input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();radio_uncheck();" value="CorporateEmails"/>
Corporate Press Release Emails<br />
<input type="text" id="atext">
Comments
Assuming these render on the browser as a normal checkbox and textarea, then you can get notification of when the checkbox is ticked or unticked using its click event:
$("selector_for_the_checkbox").click(function() {
// ...handle the event here...
});
Within the event handler, this will refer to the raw checkbox element (not a jQuery wrapper for it).
To show a hidden element with jQuery, you use show; to hide one, you use hide. You can also use toggle to show or hide the element on the basis of its current state or a flag you pass in. So:
$("selector_for_the_checkbox").click(function() {
$("selector_for_the_textarea").toggle(this.checked);
});
It's well worth spending an hour (that's all it takes) reading through the jQuery API documentation beginning to end.
1 Comment
try this script:
<script type="text/javascript">
function toggleTextBox(bEnable, textBoxID) {
document.getElementById(textBoxID).disabled = !bEnable;
}
}
</script>
and your aspx would be like:
<asp:CheckBox ID="chkApplied" runat="server" onclick="javascript:toggleTextBox(this.checked, 'txtAmount'); />
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
and if your client side id differs, you can bind it from server side like this:
chkApplied.Attributes.Add("onclick", "javascript:toggleTextBox(this.checked, '" + txtAmount.ClientID + "');");