12

I need to disable all the check boxes inside a table cell when clicking on a hyperlink inside the same table.

I'm using the following jquery code to select all the check boxes nested inside the table.

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
$($el).attr('checked', true);

For some reason this piece of code is not working.

Can anyone show me how to fix it?

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
user152248
  • 145
  • 1
  • 3
  • 7

7 Answers7

34
$('table input[type=checkbox]').attr('disabled','true');

if you have an id for the table

$('table#ID input[type=checkbox]').attr('disabled','true');
Tzury Bar Yochay
  • 8,798
  • 5
  • 49
  • 73
  • 3
    I have noticed that one should take write true and not 'true'. This works : $('table input[type=checkbox]').attr('disabled',true) but not this : $('table input[type=checkbox]').attr('disabled','true'); – gpasse Nov 03 '11 at 14:49
  • Helped me out and saved me from writing up a question. Thank you. +1 – SearchForKnowledge Jun 10 '15 at 15:39
6

Disable?

$("a.clickme").click(function(){
  $(this)                    // Link has been clicked
    .closest("td")           // Get Parent TD
    .find("input:checkbox")  // Find all checkboxes
    .attr("disabled", true); // Disable them
});

or Checked?

$("a.clickme").click(function(){
  $(this)                    // Link has been clicked
    .closest("td")           // Get Parent TD
    .find("input:checkbox")  // Find all checkboxes
    .attr("checked", false); // Uncheck them
});
Sampson
  • 265,109
  • 74
  • 539
  • 565
2

Your code can be a lot simpler:

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');

Could be:

$el = $(this).parents('table:first :checkbox');

Then to disable them:

$el.attr('disabled', 'disabled');

or to check them:

$el.attr('checked', 'checked');

or to uncheck them:

$el.removeAttr('checked');

or to enable them:

$el.removeAttr('disabled');
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
0

See also: selector/checkbox

jQuery("#hyperlink").click(function() {
  jQuery('#table input:checkbox').attr('disabled', true);
  return false;
});
ranonE
  • 497
  • 2
  • 6
0

This is my solution

// Action sur le checkbox
        $("#tabEmployes thead tr th:first input:checkbox").click(function() {
            var checked = $(this).prop('checked');
            $("#tabEmployes tbody tr td:first-child input:checkbox").each(function() {
                $(this).prop('checked',checked);
            });
        });
m4dd
  • 89
  • 1
  • 2
  • 7
0

------------------------------- HTML Code below ------------------------------

<table id="myTable">
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>

<input type="button" onclick="callFunction()" value="Click" />

------------------------------- JQuery Code below -----------------------------

    <script type="text/javascript">
    function callFunction() {
        //:
        $('table input[type=checkbox]').attr('disabled', 'true');
    }
</script>
Raja Danish
  • 235
  • 1
  • 7
  • Hi, after clicking on button, you will see that when this JS function will call, all the four check-boxes will become disabled. Thanks. – Raja Danish Feb 20 '14 at 12:49
0

// Enable/Disable All Checkboxes

$('#checkbox').click(function() {

    var checked = $(this).attr('checked');
    var checkboxes = '.checkboxes input[type=checkbox]';

    if (checked) {
        $(this).attr('checked','checked');
        $(checkboxes).attr('disabled','true');
    } else {
        $(this).removeAttr('checked');
        $(checkboxes).removeAttr('disabled');
    }
});
Brynner Ferreira
  • 1,527
  • 1
  • 21
  • 21