3

Fiddle - http://jsfiddle.net/XH5zm/
JSBin - http://jsbin.com/UKIweJE/1/edit

I'm trying to enable/disable an input textbox by toggling if a checkbox is checked or not.

$(document).ready(function() {
  if ($('.enable-padding').attr('checked') === true) {
    $('.grab-padding').attr('enabled','enabled');
    $('.grab-padding').val("13px");
  } else {
    $('.grab-padding').attr('disabled','disabled');
    $('.grab-padding').val(" ");
  }
});

What exactly am I'm doing wrong?

Any help is greatly appreciated.

3 Answers 3

5
  1. You don't listen to the change event.
  2. There is no enabled property.
  3. .attr() doesn't return a boolean value and it doesn't change the properties.

    $('.enable-padding').on('change', function() {
        $('.grab-padding').prop('disabled', !this.checked)
                          .val(this.checked ? "13px" : "");
    });
    
Sign up to request clarification or add additional context in comments.

Comments

1

You have to add an event for change the state of checkbox:

$(document).ready(function() {
  $('.enable-padding').on('change',function(){
    if ($('.enable-padding').is(':checked')) {
    $('.grab-padding').removeAttr('disabled');
    $('.grab-padding').val("13px");
  } else {
    $('.grab-padding').attr('disabled','disabled');
    $('.grab-padding').val(" ");
  }
  });
});

and modifiy the input for this:

<input class="grab-padding" type="text" value="13px"></td>

Comments

0
$('#chbox').click(function () {
  if ( $('#txt_data').attr('disabled') =='disabled' ){
      $('#txt_data').removeAttr('disabled');
  }else{
         $('#txt_data').attr('disabled','disabled');
  }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.