Search code examples
javascriptjquerycheckboxchecked

checkbox enable/disable from jquery


I have a question for onclick action. After clicking the "all" checkbox, <select> is blocked, but no "cl" is unchecked.

<form>
  <input type="checkbox" class="custom-control-input" id="all" name="all" onclick="this.form.elements['oh'].disabled = this.form.elements['cl'].removeAttr('checked') = this.checked">

  <select id="oh" name="oh">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
    <option value="3">oprion 3</option>
  </select>

  <input type="checkbox" id="cl" name="cl">
</form>


Solution

  • I believe you MIGHT mean this

    $("#all").on("click",function() {
      const chk = this.checked;
      $("#oh").prop("disabled",chk)
      $("#cl").prop('checked',!chk)
    })  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form>
      <input type="checkbox" class="custom-control-input" id="all" name="all" />
    
      <select id="oh" name="oh">
        <option value="1">option 1</option>
        <option value="2">option 2</option>
        <option value="3">oprion 3</option>
      </select>
    
      <input type="checkbox" id="cl" name="cl">
    </form>