Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used below coding to enable/disbale textbox when user check checkbox. by default it should be disable when user check it have to be enable. i tried this but not working.
(Note: by default its in disable mode but when i check the checkbox still in disable mode)

XML
<script src="../js/jquery.min.js" type="text/javascript"></script>
    <script src="../js/global-script.js" type="text/javascript"></script>
    <script src="../js/jquery-1.5.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
       $(document).ready(function() {
 $("input[type=textbox][id*=txtfrmDate]").attr("disabled", true);

$("input[type=checkbox][id*=ChkDate]").click(function() {
if (this.checked)
    $(this).closest("tr").find("input[type=text][id*=txtfrmDate]").attr("disabled", false);
else
    $(this).closest("tr").find("input[type=text][id*=txtfrmDate]").attr("disabled", true);
});
});





XML
<asp:TextBox ID="txtfrmDate" runat="server" CssClass="txtbox datebox"  Style="z-index: 98; width: 100px;">          </asp:TextBox>
Posted
Comments
Darkness_07 13-Mar-13 10:24am    
("Your codes")-> $(this).closest("tr").find("input[type=text][id*=txtfrmDate]").attr("disabled", false);

I think you miss this "box" in your input[type=text]

1 solution

Hello Umapathi,

I have created a sample HTML using JQuery to show you how it is done.
HTML
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/Javascript"></script>
<script type="text/JavaScript">
function enableName(ctrl, txtId) {
if (ctrl.checked)
    $('#' + txtId).removeAttr('disabled');
else
    $('#' + txtId).attr('disabled', 'disabled');
}
</script>
</head>
<body>

<form action="demo_form.asp">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"  id="lname" disabled><input type="checkbox" id="chkName" name="chkName" onclick="enableName(this, 'lname');"/><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>
</br></br>

Basically you will have to use javascript code similar to shown below to enable the textbox.
JavaScript
$('#' + txtId).removeAttr('disabled');
To again disble the textbox use javascript code similar to shown below.
JavaScript
$('#' + txtId).attr('disabled', 'disabled');
Also from your code I suggest that you use direct ID of the textbox rather than using closest & find. Remember ID's are unique in a page. i.e. No two controls should have same id.

Regards,
 
Share this answer
 
Comments
Umapathi K 14-Mar-13 0:48am    
its working fine in mozilla but not in IE and Chrome..
Prasad Khandekar 14-Mar-13 1:54am    
Hello Umapathi,

Have u tried using id to locate the textbox. Are u getting any errors? For firefox please check the error console Ctrl +Shift +J. May be your javascript code is not finding the control. The sinippet I have posted works in IE 8, Mozilla & Chrome.

Regards,
Umapathi K 14-Mar-13 5:03am    
its not working
Prasad Khandekar 14-Mar-13 5:18am    
Hello Umapathi,

It seems that you are using a way to old jquery library. Can you try using the latest which is 1.9.1. Also did you see any error in IE Developer ToolBar Console window or Chrome Browsers Console Window.

Regards,
Umapathi K 14-Mar-13 5:21am    
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/Javascript"></script>
using this.,
in console i am not getting any error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900