1

In the below code initially checkbox unchecked and textboxes disabled.when i click checkbox then textboxes enabled BUT after the enabled when i again unchecked checkboxes it need to be disabled.But that partis not working(else part) Code

 $(function(){
    $("#txtres").attr("disabled", "disabled");
    $("#txtres2").attr("disabled", "disabled");

    $("#chknew").click(function () {
        if("#chknew").is(:checked)
        {
        $("#txtres").removeAttr("disabled");
        $("#txtres2").removeAttr("disabled");
        }
      else
      {
         $("#txtres").attr("disabled", "disabled");
         $("#txtres2").attr("disabled", "disabled");
        }
    });
    }
2
  • ohhhh I see, aren't you getting an error your if has a missing ) in last. Commented Aug 24, 2014 at 16:17
  • .is(:checked) => .is(':checked') Commented Aug 24, 2014 at 16:45

3 Answers 3

1

You can achieve it with "change" handler

$(function(){
   $("#txtres").attr("disabled", "disabled");
   $("#txtres2").attr("disabled", "disabled");

$('#chknew').change(function () {
   if ($(this).is(":checked")) {
       $("#txtres").removeAttr("disabled");
       $("#txtres2").removeAttr("disabled");
   } else {
       $("#txtres").attr("disabled", "disabled");
       $("#txtres2").attr("disabled", "disabled");
   }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

To make it again disabled, Say like bellow

$("#txtres").prop("disabled", true);

instead of

$("#txtres").attr("disabled", "disabled");

3 Comments

You should use .prop() instead. .attr() is not suitable for boolean attributes like disabled, readonly and the likes.
@Terry updated my answer. Reading about how it helps.
In my code disabled partis working,Butin the else part coding not working
0
$(function(){
    $("#txtres").attr("disabled", "disabled");
    $("#txtres2").attr("disabled", "disabled"); 
    $("#chknew").click(function () {    
        $("#txtres").attr("disabled", !this.checked);
        $("#txtres2").attr("disabled", !this.checked);          
    });
});

Demo:

http://jsfiddle.net/w2jtg1b9/3/

1 Comment

When i checked it i wantedto enabled the TextBoxes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.