Skip to content Skip to sidebar Skip to footer

Enable Disable Inputs Text With Its Corresponding Checkbox Jquery

Having several rows, where each row has a checkbox, a label, and an input text How can I enable/disable the checkbox's neighbour input text when the check box is selected, using jq

Solution 1:

maybe like this:

$(':checkbox').change(function(){
   var this_nr=$(this).attr('id').substr(-1);
   $('#input'+this_nr).prop('disabled', !$(this).is(':checked'));
});
$(':checkbox').change();
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><form><p><inputid="chk1"type="checkbox"><label >text 1 </label><inputid="input1"type="text"></p><p><inputid="chk2"type="checkbox"><label >text 2 </label><inputid="input2"type="text"></p><p><inputid="chk3"type="checkbox"><label >text 3 </label><inputid="input3"type="text"></p></form>

or like this:

$(':checkbox').change(function(){
   $('input:text').eq($(':checkbox').index(this)).prop("disabled", !$(this).is(':checked'));
});
$(':checkbox').change();
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><form><p><inputid="chk1"type="checkbox"><label >text 1 </label><inputid="input1"type="text"></p><p><inputid="chk2"type="checkbox"><label >text 2 </label><inputid="input2"type="text"></p><p><inputid="chk3"type="checkbox"><label >text 3 </label><inputid="input3"type="text"></p></form>

Solution 2:

You could use the jQuery siblings function to retrieve the neighboring textbox and enable/disable it based on the .checked property. I've called .change() at the end to set the initial state of the textboxes.

$(":checkbox")
  .change(function() {
    $(this)
      .siblings("input[type='text']")
      .prop("disabled", !this.checked);
  }).change();
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form><p><inputid="chk1"type="checkbox"><label>text 1 </label><inputid="input1"type="text"></p><p><inputid="chk2"type="checkbox"><label>text 2 </label><inputid="input2"type="text"></p><p><inputid="chk3"type="checkbox"><label>text 3 </label><inputid="input3"type="text"></p></form>

Solution 3:

I would use event delegation on the form, in the handler using nextAll to find the next text box.

// disable all the text fields
$('#myform :text').each(function () {
  $(this).prop('disabled', true);
});

$('#myform').on('change', ':checkbox', function (evt) {
  let textbox = $(this).nextAll(':text');
  textbox.prop('disabled', (i, val) => !val);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="myform"><p><inputid="chk1"type="checkbox"><labelfor="chk1">text 1 </label><inputid="input1"type="text"></p><p><inputid="chk2"type="checkbox"><labelfor="chk2">text 2 </label><inputid="input2"type="text"></p><p><inputid="chk3"type="checkbox"><labelfor="chk3">text 3 </label><inputid="input3"type="text"></p><p><labelfor="tos">I agree to the Terms of Service</label><inputid="tos"type="checkbox"><br><labelfor="comment">Comments </label><inputid="comment"type="text"value="This will toggle when you check the TOS"></p></form>

This works if your entire form is in this checkbox followed by textbox form; it will break if you have any other checkboxes that don't control a textbox or textboxes that shouldn't be disabled. To be more flexible, I would add a class to the items I wanted to have this behavior:

// disable all the text fields
$('#myform :text.toggle-input').each(function () {
  $(this).prop('disabled', true);
});

$('#myform').on('change', ':checkbox.toggle-input', function (evt) {
  let textbox = $(this).nextAll(':text.toggle-input');
  textbox.prop('disabled', (i, val) => !val);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="myform"><p><inputid="chk1"class="toggle-input"type="checkbox"><labelfor="chk1">text 1 </label><inputid="input1"class="toggle-input"type="text"></p><p><inputid="chk2"class="toggle-input"type="checkbox"><labelfor="chk2">text 2 </label><inputid="input2"class="toggle-input"type="text"></p><p><inputid="chk3"class="toggle-input"type="checkbox"><labelfor="chk3">text 3 </label><inputid="input3"class="toggle-input"type="text"></p><p><labelfor="tos">I agree to the Terms of Service</label><inputid="tos"type="checkbox"><br><labelfor="comment">Comments </label><inputid="comment"type="text"value="This won't toggle when you check the TOS"></p></form>

Post a Comment for "Enable Disable Inputs Text With Its Corresponding Checkbox Jquery"