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>

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

    ReplyDelete