Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello...
i have problem to solve on my script. somebody help me please..

i have script:
HTML
<!DOCTYPE html>
<html>
<head>
    <title>jQuery With Example</title>
    @Scripts.Render("~/bundles/jquery")
    <script type="text/javascript">
        $(function () {

            $('.chkview-1').change(function () {
                $('.chkitem-1').prop('checked', $(this).is(':checked'));
            });

            $(".chkitem-1").change(function () {
                $('.chkview-1').prop('checked', $('.chkitem-1:checked').length == $('.chkitem-1').length);
            });

            
        });
    </script>
</head>
<body>
    <table id="mytable">
        <tr>
            <td>
                View
            </td>
            <td>Insert</td>
            <td>Update</td>
            <td>Delete</td>
        </tr>
        <tr>
            <td>Administrator</td>
            <td>
                <input type="checkbox" class="chkview-1" />
            </td>
            <td><input type="checkbox" class="chkitem-1"/></td>
            <td><input type="checkbox" class="chkitem-1"/></td>
            <td><input type="checkbox" class="chkitem-1"/></td>
        </tr>
        <tr>
            <td>Free User</td>
            <td>
                <input type="checkbox" class="chkview-2" />
            </td>
            <td><input type="checkbox" class="chkitem-2"/></td>
            <td><input type="checkbox" class="chkitem-2"/></td>
            <td><input type="checkbox" class="chkitem-2"/></td>
        </tr>
    </table>
</body>
</html>


in this case, i'll explain litle of my code, chkitem-2 is chkitem=classid and 2 dynamically render role id in one row like administrator so i create classid="chkitem-2" or chkview-2.

in this code if i click chkview-1 all item chkitem-1 in row administrator checked true. can i create dynamically if i click chkview-2, all chkitem-2 also checked true in row free user?

somebody, help me please..
Posted

you can do as below

JavaScript
$('input[type=checkbox]').change(function (event) {
   var obj = $(event.target).attr('class');
   if (obj.indexOf("chkview") == 0) {
       var c1 = obj.replace("chkview", ".chkitem")
       $(c1).prop('checked', $(this).is(':checked'));
   } else if (obj.indexOf("chkitem") == 0) {
       var c11 = "." + obj;
       var c2 = obj.replace("chkitem", ".chkview");
       $(c2).prop('checked', $(c11 + ':checked').length == $(c11).length);
   }
});


DEMO[^]
 
Share this answer
 
v2
Comments
Ade.Ruyani.Z 18-May-14 23:44pm    
Thank you damitSL..
i solve by my self,

HTML
<!DOCTYPE html>
<html>
<head>
    <title>jQuery With Example</title>
    @Scripts.Render("~/bundles/jquery")
    <script type="text/javascript">
        $(function () {

            $('.chkview').change(function () {
                $(this).closest('tr').find('.chkitem').prop('checked', this.checked);
            });

            $(".chkitem").change(function () {
                var $tr = $(this).closest('tr'), $items = $tr.find('.chkitem');
                $tr.find('.chkview').prop('checked', $items.filter(':checked').length == $items.length);
            });

            
        });
    </script>
</head>
<body>
    <table id="mytable">
        <tr>
            <td>View</td>
            <td>Insert</td>
            <td>Update</td>
            <td>Delete</td>
        </tr>
        <tr>
            <td>Administrator</td>
            <td>
                <input type="checkbox" class="chkview chkview-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-1" />
            </td>
        </tr>
        <tr>
            <td>Free User</td>
            <td>
                <input type="checkbox" class="chkview chkview-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
        </tr>
    </table>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900