Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a checkboxes(3) checked at one click using jquery

Here i have bunch of checkboxes,

say, if i clicked on any checkbox, i want make checked next two checkboxes also (i.e, beside two)

<table border="1" cellspacing="0" width="450">
    <tr>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>

Fiddle

could anyone help me out,

Thanks in advance.

like image 386
user123456789 Avatar asked Feb 03 '26 07:02

user123456789


2 Answers

try with nextAll() and eq to select next two.

$('input:checkbox').change(function(){
  var obj=$(this).parent().nextAll(':eq(0),:eq(1)'); //eq(0) and (1) is the index of next two checkbox
  obj.find(':checkbox').prop('checked',this.checked)
});

fiddle here

this uncheck the checkbox if unchecked..

like image 122
bipen Avatar answered Feb 04 '26 22:02

bipen


$(":checkbox").click(function () {
    $(this).parent().nextAll("td").slice(0, 2).find(":checkbox").prop('checked', true);
});

JSFIDDLE

like image 28
Barmar Avatar answered Feb 04 '26 21:02

Barmar