Translate

Thursday, August 9, 2012

check all checkboxes using jquery

I wrote this code that checks all checkboxes in a table.











<html>
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
    <table id="mytable">
        <tr>
            <td>
                <input id="chkParent" type="checkbox" />
            </td>
            <td>
                Parent
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox2" type="checkbox" />
            </td>
            <td>
                1
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox3" type="checkbox" />
            </td>
            <td>
                2
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox4" type="checkbox" />
            </td>
            <td>
                3
            </td>
        </tr>
    </table>
    <script type="text/javascript">

        $(document).ready(function () {
       

            $('#chkParent').click(SetKids);

            function SetKids() {

                  var isChecked = this.checked; 


                $('#mytable').find(':checkbox').attr('checked', isChecked);
            }

        });

    </script>

</body>
</html>






You can copy paste this code into a text file and name the file with an htm extension. Where ever you decide to put the htm file, create a folder called Scripts. In that folder put this file.

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator