0

I have some code that is working great, when a check box is checked, it disables three fields and sets values for those fields.

$(document).ready(function () {
    $("#checkbox_1").change(function () { 
        $("#textbox_1").attr("disabled", $(this).attr("checked"));
        $("#textbox_2").attr("disabled", $(this).attr("checked"));
        $("#textbox_3").attr("disabled", $(this).attr("checked"));
        $("#textbox_1").val(1);
        $("#textbox_2").val("Classic");
        $("#textbox_3").val(1);
    });
});

My question is, how do I make it so I can set the values of those three fields back when the box is unchecked?

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
user1789437
  • 490
  • 1
  • 7
  • 22

1 Answers1

4

Use element properties instead of attributes to set the current state of the element:

$(document).ready(function () {
   $("#checkbox_1").change(function () { 
      $("#textbox_1").prop("disabled", $(this).prop("checked"));
      $("#textbox_2").prop("disabled", $(this).prop("checked"));
      $("#textbox_3").prop("disabled", $(this).prop("checked"));
      $("#textbox_1").val(1);
      $("#textbox_2").val("Classic");
      $("#textbox_3").val(1);
   });
});
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
Musa
  • 96,336
  • 17
  • 118
  • 137
  • this should work in conjunction with toggle()... also I like to use .prop("checked", true); or .prop("checked", false); to be more explicit... – carter Jul 31 '13 at 01:22