2

How do I disabled textbox if checkbox is checked. I've got a few textboxes and checkboxes next to each other.

HTML:

<form id="form">
<input type="checkbox" name="deposit_checked"> <input type="text" name="deposit_value">
<input type="checkbox" name="booking_checked"> <input type="text" name="booking_value">
<input type="checkbox" name="referral_checked"> <input type="text" name="referral_value">
</form>

I can do it individually with code below:

$("input[name=deposit_checked]").change(function(){

if( $("input[name=deposit_checked]").is(":checked") ) {
    $("input[name=deposit_value]").attr("disabled", false);
} else {
    $("input[name=deposit_value]").attr("disabled", true);
}

})

to save time, I tried using .each() and .next() function but no luck:

$("#form input[type=checkbox]").each(function() { 
$(this).change(function(){
   if( $(this).is(":checked") ) {
  $(this).next().attr("disabled", false);
   } else {
  $(this).next().attr("disabled", true);
   }
})
})
1
  • Your code works, what is the problem? Commented Oct 19, 2011 at 6:31

4 Answers 4

3

To minimize DOM objects selection, cover inputs in somethings like span.

You can see this example here: http://jsfiddle.net/g4mQR/

<form id="form">
  <span>
    <input type="checkbox" name="deposit_checked"> 
    <input type="text" name="deposit_value">
  </span>
  <span>
    <input type="checkbox" name="booking_checked"> 
    <input type="text" name="booking_value">
  </span>
  <span>
    <input type="checkbox" name="referral_checked"> 
    <input type="text" name="referral_value">
  </span>
</form>

JS code

$('#form input[type=checkbox]').click(function(){
  var obj = $(this).siblings('input[type=text]');
  obj.attr('disabled', !obj.attr('disabled'));
})
Sign up to request clarification or add additional context in comments.

Comments

3

You were setting wrong value for the disabled attribute. Here is the fix:

$("#form input[type=checkbox]").each(function() { 
$(this).change(function(){
   if( $(this).is(":checked") ) {
  $(this).next().removeAttr("disabled");
   } else {
  $(this).next().attr("disabled", "disabled");
   }
})
})

Here is the working example at jsfiddle. Note that the all the javascript should be declared in the $(window).load() handler.

1 Comment

Please see the updated post with reference to the working example at jsfiddle.
1

This should do:

$('#form input[type="checkbox"]').change(function() {
   var t = $(this);
    t.next().attr('disabled', ! t.is(':checked'));
});

Remember to set the fields' and checkboxes' state in the beginning. For example: all unchecked and disabled.

+You may want to change the #form bit in the selector.

Comments

1

i guess you want to hide n show the texbox respective to its checkbox, so you can do this by attaching change event, this is how it is done: change event handler is fired every time the value of the check box is changed

$(document).ready(function() {
   $('.chkboxClass').bind('change', function () {

   if ($(this).is(':checked'))
     $(this).next('.txtboxclass').hide();
   else
     $(this).next('.txtboxclass').show();

  });
});

where .chkboxclass is class for all checkboxes and .txtboxclass is class for all textboxes

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.