1

how to change the dropdown based on the checkbox selection.

function checbocChecked(event) {
  let check = document.querySelectorAll('input[type=checkbox]:checked').length;
  alert(check)
  if (check > 1) {
    //here how can i manipulate all the dropdown such that it become MULTIPLE
  } else {
    //here how can i manipulate all the dropdown such that it become SINGLE
  }
}
<table>
  <thead>
    <tr>
      <td>Checbox</td>
      <td>Key</td>
      <td>Value</td>
    </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="checkbox" onclick="checbocChecked(event)"></td>
        <td><input type="numeric" value=1></td>
        <td>
          <select>
            <option value="SINGLE">SINGLE</option>
            <option value="MULTIPLE">MULTIPLE</option>
          </select>
      </tr>
      <tr>
        <td><input type="checkbox" onclick="checbocChecked(event)"></td>
        <td><input type="numeric" value=2></td>
        <td>
          <select>
            <option value="SINGLE">SINGLE</option>
            <option value="MULTIPLE">MULTIPLE</option>
          </select>
      </tr>
    </tbody>
</table>

<div id="final"></div>

If single check box is selected then all the dropdown in the table should hold SINGLE as a option value if more than one checkbox is selected then all the dropdown will hold MULTIPLE as a option value

what i tried is above

1

1 Answer 1

1

Just loop through the select fields and update value accordingly.

function checbocChecked(event) {
  let check = document.querySelectorAll('input[type=checkbox]:checked').length;
  alert(check)
  if (check > 1) {
    //here how can i manipulate all the dropdown such that it become MULTIPLE
    let elements = document.querySelectorAll('select');
    for (var i=0; i < elements.length; i++) {
      elements[i].value = "MULTIPLE";
    }    
  } else {
    //here how can i manipulate all the dropdown such that it become SINGLE
    let elements = document.querySelectorAll('select');
    for (var i=0; i < elements.length; i++) {
      elements[i].value = "SINGLE";
    }
  }
}
<table>
  <thead>
    <tr>
      <td>Checbox</td>
      <td>Key</td>
      <td>Value</td>
    </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="checkbox" onclick="checbocChecked(event)"></td>
        <td><input type="numeric" value=1></td>
        <td>
          <select>
            <option value="SINGLE">SINGLE</option>
            <option value="MULTIPLE">MULTIPLE</option>
          </select>
      </tr>
      <tr>
        <td><input type="checkbox" onclick="checbocChecked(event)"></td>
        <td><input type="numeric" value=2></td>
        <td>
          <select>
            <option value="SINGLE">SINGLE</option>
            <option value="MULTIPLE">MULTIPLE</option>
          </select>
      </tr>
    </tbody>
</table>

<div id="final"></div>

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

2 Comments

Thanks @iamdlm what if i have other checkbox presest in dom but not in table can i use this let check = document.querySelectorAll('table tbody tr input[type=checkbox]:checked').length; so that only checkbox inside of table should be picked
Yes, same for select.

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.