Wednesday 30 January 2013

Enable/Disable textbox on checkbox click using javascript


//------------- Javascript ----------------------

<script type="text/javascript" language="javascript">

function CheckCheckboxes1(chk){
 
    if(chk.checked == true)
    {
        var txt = document.getElementById('TextBox1');
        txt.value = "";
        txt.disabled = false;
    }
    else
    {
        var txt = document.getElementById('TextBox1');
        txt.value = "Enter Username";
        txt.disabled = true;
    }

    OR

   //--------If you are working with master page then use following code to do the same

  //------ Disable textbox
  $("[id$='TextBox1']").prop('disabled',true);

  //------ Enable textbox
  $("[id$='TextBox1']").prop('disabled',false);

  OR
  //------ Disable textbox
  $("[id$='TextBox1']").attr("disabled", "disabled");

  //------ Enable textbox
  $("[id$='TextBox1']").removeAttr("disabled");

}
</script>

//------------- HTML ----------------------

<asp:CheckBox ID="chk_NetBanking" runat="server" onclick="CheckCheckboxes1(this)" /> <br /> <asp:TextBox ID="TextBox1" runat="server" Width="250px" Enabled="false" Text="Enter Username" MaxLength= "20"></asp:TextBox>

No comments:

Post a Comment