1

I have a table which can have variable # of rows. how can I disable all checkboxes at once?

   <table border="1"  class="myTable grid">
    <tr align="center">
    <td>&nbsp;</td>
    <td>A</td>
    </tr>
    <tr align="center">
    <td>1</td>
    <td><input type="checkbox" name="cb1;1" value="1"></td>
    </tr>
    <td>2</td>
    <td><input type="checkbox" name="cb2;1" value="1" checked></td>
    </tr>
    <tr align="center">
     </table>
6
  • 1
    If you could use jQuery, $('.myTable input[type=checkbox]').attr('disabled', 'disabled') would do the trick. Commented Aug 20, 2015 at 23:53
  • @JohnBupit perfect!! Thank you! Commented Aug 20, 2015 at 23:57
  • Please mark your question as solved. Commented Aug 21, 2015 at 0:03
  • @pyb how do I mark it solved? Thanks! Commented Aug 21, 2015 at 0:21
  • @Kaur there should be a button or tick on the left of each answer. You can only pick one. Commented Aug 21, 2015 at 0:39

3 Answers 3

1

Here is a javascript only solution that disables all checkboxes in myTable.

document.getElementByClassName("myTable").querySelectorAll("input[type='checkbox']").disabled = true;
Sign up to request clarification or add additional context in comments.

1 Comment

It's document.getElementsByClassName()
1

A jQuery solution would be:

$('.myTable input[type=checkbox]').attr('disabled', 'disabled');

Alternatively:

document.querySelectorAll('.myTable input[type=checkbox]').disabled = true;

Comments

1

Try utilizing a for loop to iterate NodeList returned by document.querySelectorAll(selectors) , set each [type=checkbox] element to disabled within for statement

var inputs = document.querySelectorAll("[type=checkbox]");
for (var i = 0; i < inputs.length; i++) {
  inputs[i].disabled = true;
}
<table border="1" class="myTable grid">
  <tbody>
    <tr align="center">
      <td>&nbsp;</td>
      <td>A</td>
    </tr>
    <tr align="center">
      <td>1</td>
      <td>
        <input type="checkbox" name="cb1;1" value="1">
      </td>
    </tr>
    <td>2</td>
    <td>
      <input type="checkbox" name="cb2;1" value="1" checked>
    </td>
    </tr>
    <tr align="center">
  </tbody>
</table>

1 Comment

I have more checkboxes on the form with type checkbox; hence I am targeting these only with table. Thanks for your input.@guest271314

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.