39

I have a list with a checkbox and some text (like email's inbox). I want to check/uncheck the checkbox when i click anywhere on the list item (row). I have used following code on click event of the row:

$(this).find(".cbx input").attr('checked', true);

For first time, it show the checked checkbox. But when I click select it next time the value of checkbox checked="checked" updates in the html code (i checked through firebug) but the checkbox does not displayed as checked.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Gaurav
  • 1,214
  • 2
  • 17
  • 32

7 Answers7

100

With .attr() you set the value of an attribute in the DOM tree. Depending on the browser, browser version and jQuery version (especially 1.6), this does not always have the desired effect.

With .prop(), you manipulate the property (in this case, 'checked state'). I strongly recommend using .prop() in your case, so the following will toggle correctly:

$(this).find(".cbx input").prop('checked', true);

For further information, please refer to the documentation of .prop().

UweB
  • 4,080
  • 2
  • 16
  • 28
  • I used ("#id_of_object").prop('checked', true); to be more specific, and find didn't work for me with the id I wanted (not using class). Thanks! – radtek Sep 04 '14 at 19:21
  • Thanks, i didn't understand why it didn't display as checked until i read your solutions. – Khanh Pham Dec 04 '15 at 10:13
  • Oh wow, and here I was thinking that .attr was just a deprecated version of .prop!! Turns out they are fundamentally different! (although I question why??) – beliha Jul 25 '18 at 11:11
9

Almost right, just use 'checked' instead of true:

$(this).find(".cbx input").attr('checked', 'checked');

Update: Alternatively, you can use this with newer JQuery versions:

$(this).find(".cbx input").prop('checked', true);
Rob
  • 11,492
  • 14
  • 59
  • 94
2

Easy, the solution is to use $('xxxxxx').prop('checked', true) or false Test OK in Jquery 1.11.x.

Cheers

FULL DEMO CODE EXAMPLE

Examples: (Code JavaScript checkbox for a table)

    function checkswich() {
        if ($('#checkbox-select').is(':checked') == true) {
            selectTodo();
        } else {
            deSelectTodo();
        }
    }
    function selectTodo() {
        //$('#tabla-data input.ids:checkbox').attr('checked', 'checked');
        $('#tabla-data input.ids:checkbox').prop('checked', true);
    }
    function deSelectTodo() {
        //$('#tabla-data input.ids:checkbox').removeAttr('checked', 'checked');
        $('#tabla-data input.ids:checkbox').prop('checked', false);
    }
    function invetirSelectTodo() {
        $("#tabla-data input.ids:checkbox").each(function (index) {
            //$(this).attr('checked', !$(this).attr('checked'));
            $(this).prop('checked', !$(this).is(':checked'));
        });
    }

Examples: (Code HTML checkbox for a table)

           <table id="tabla-data" class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 5px"><span class="glyphicon glyphicon-check"></span></th>
                        <th>Engine</th>
                        <th><a href="#" class="">Browser</a></th>
                        <th class="td-actions text-center" style="width: 8%;">Acciones</th>
                    </tr>
                </thead>
                <tbody id="tabla-data" class="checkbox-data">
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                </tbody>
            </table>

Example: (Code ComponentHTML use JavaScript)

<form id="form-datos" action="/url/demo/blablaaa" method="post" enctype="multipart/form-data" style="visibility: hidden;" >
<input type="hidden" name="accion" id="accion">

<div class="btn-group">
<span class="btn btn-default btn-xs"><input type="checkbox" id="checkbox-select" onclick="checkswich()"></span>
<button type="button" class="btn btn-default btn-xs dropdown-toggle dropdown-toggle-check" data-toggle="dropdown" aria-haspopup="true"
        aria-expanded="false">
    <span class="caret"></span>
    <span class="sr-only">Options</span>
</button>
<ul class="dropdown-menu">
    <li><a href="#" onclick="accion()">Action Demo</a></li>
    <li role="separator" class="divider"></li>
    <li><a href="#" onclick="selectTodo()">Select All</a></li>
    <li><a href="#" onclick="deSelectTodo()">UnSelet All</a></li>
    <li><a href="#" onclick="invetirSelectTodo()">Inverse Select</a></li>
</ul>

Anupam
  • 14,950
  • 19
  • 67
  • 94
Sergio
  • 2,369
  • 1
  • 15
  • 22
1
useing **.prop()** it worked for me !

$(document).on("click",".first-table tr",function(e){
if($(this).find('input:checkbox:first').is(':checked'))
{
    $(this).find('input:checkbox:first').prop('checked',false);
}else
{
    $(this).find('input:checkbox:first').prop('checked', true);
}
});
mohitesachin217
  • 451
  • 5
  • 14
1

nothing initially worked for me in chrome though it was working fine in mozilla. Later found out that custom CSS styling caused the checkmark to be not visible.

Suhas Jain
  • 43
  • 1
  • 7
0

This solution did not work for me (ie. Setting the checked state with prop).

$("#checkbox-reminder").prop("checked", true);

While the underlying state always updated correctly, the display would not properly repaint.

This can be solved with.

$("#checkbox-reminder").prop("checked", true).checkboxradio();
$('#checkbox-reminder').checkboxradio('refresh');

See this thread for more info.

Olmstov
  • 563
  • 7
  • 18
-1
 $("[id*='" + value + "'] input").prop('checked', false);

attr also worked (but first time only) But prop always works.

StuartLC
  • 104,537
  • 17
  • 209
  • 285