0

I have a checkbox/select and want the select to be enabled when the checkbox is checked and disabled when unchecked.

<p>
<input id="option[21]" type="checkbox" name="checkbox[21]">
<select id="option[21]" name="select[21]" disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

<p>
<input id="option[22]" type="checkbox" name="checkbox[22]">
<select id="option[22]" name="select[22]"  disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

When I run my code, it doesn't appear to select the dropdown correctly:

$(document).on('change', 'input[type=checkbox]', function(event) {
  var selectId = event.target.id;
  console.log(selectId);

  if ($(this).is(':checked')) {
    $(selectId).removeAttr('disabled');
  } else {
    $(selectId).prop( "disabled", true );
  }
});

https://jsfiddle.net/rL3ghb0f/

1
  • I took too long to answer here's the jsfiddle: jsfiddle.net/rL3ghb0f, I kept your second checkbox the same but got your first checkbox is the one I worked on. Your ID's were repeated and the squared brackets caused alot of console errors, try refraining from that. Commented May 5, 2021 at 0:09

2 Answers 2

1

First of all, your HTML is invalid. ID attributes of your nodes must be unique in the scope of a document. Having <input> and <select> tags sharing the same ID is violation of that rule.

Secondly, you're using the ID property taken from one node (line 2) to select another node (lines 6 and 8). If there are multiple objects sharing the same ID, how is jQuery supposed to know which one do you mean?

Thirdly, your CSS selector is wrong. You write ID selector with preceding hash symbol (#) https://api.jquery.com/id-selector/

I suggest to:

  1. Introduce unique id properties for <input> and <select> tags, but sharing the same number inside, e.g. <input id="21"> and <select id="option-21>
  2. Fix your CSS selectors. If you decide to follow the convention above, the selector should be $('#option-' + selectId)

See the code here:

$(document).on('change', 'input[type=checkbox]', function(event) {
  var selectId = event.target.id;
  console.log(selectId);

  if ($(this).is(':checked')) {
    $('#option-' + selectId).removeAttr('disabled');
  } else {
    $('#option-' + selectId).attr("disabled", true);
  }
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<input id="21" type="checkbox" name="checkbox[21]">
<select id="option-21" name="select[21]" disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

<p>
<input id="22" type="checkbox" name="checkbox[22]">
<select id="option-22" name="select[22]"  disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

Sign up to request clarification or add additional context in comments.

Comments

1

You have duplicate ids in your document which is not permitted in HTML5 (or any version AFAIK) and the behavior is not normative but is usually that document.getElementById and document.querySelector will return the first element.

Instead you can just use the jQuery .siblings method to traverse the DOM relative to the clicked element:

$(document).on('change', 'input[type=checkbox]', function(event) {
  let $el = $(this);
  $el.siblings('select').prop("disabled", !$el.is(':checked'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<input id="option[21]" type="checkbox" name="checkbox[21]">
<select name="select[21]" disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

<p>
<input type="checkbox" id="option[22]" name="checkbox[22]">
<select name="select[22]"  disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

2 Comments

I'm getting an Uncaught SyntaxError: Unexpected token '.' error. The source code for the page seems to be removing the variable? i.imgur.com/4rUgdoq.png
I have no idea whats going on there - its a IDE issue or something else which really has nothing to do with the code in the answer.

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.