Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hellooo... i need help, anybody help me for checkbox using javascript?
this is my script:

HTML
<!DOCTYPE html>
<html>
<head>
    <title>jQuery With Example</title>
    @Scripts.Render("~/bundles/jquery")
    <script type="text/javascript">
        $(function () {
           
            var chkId = '';
            $('.chkNumber:checked').each(function () {
                chkId += $(this).val() + ",";
            });
            chkId = chkId.slice(0, -1);
           

            $('.chkSelectAll').click(function () {
                $('.chkNumber').prop('checked', $(this).is(':checked'));
            });

        });
    </script>
</head>
<body>
    <table id="mytable">
        <tr>
            <td>
                <input type="checkbox" class="chkSelectAll" />SelectAll
            </td>
            <td><input type="checkbox" class="chkNumber" value="1" />One</td>
            <td><input type="checkbox" class="chkNumber" value="2" />Two</td>
            <td><input type="checkbox" class="chkNumber" value="3" />Three</td>
            <td><input type="checkbox" class="chkNumber" value="4" />Four</td>
            <td><input type="checkbox" class="chkNumber" value="5" />Five<br /></td>
        </tr>
    </table>
</body>
</html>



i want if i checked/unchecked all value 1-5 class(chkSelectAll) is checked/unchecked.

give me solution please...
Posted

1 solution

try below
JavaScript
$('.chkSelectAll').change(function () {
    $('.chkNumber').prop('checked', $(this).is(':checked'));
});

$(".chkNumber").change(function () {
    $('.chkSelectAll').prop('checked', $('.chkNumber:checked').length == $('.chkNumber').length);
});


DEMO[^]
 
Share this answer
 
v2
Comments
Ade.Ruyani.Z 16-May-14 12:04pm    
thank you for your answer but that's not what i mean, i mean if i checked value 1-3, checkbox class(chkSelectAll) still false checked, and if i continue checked checkbox 4-5 that mean value 1-5(all checked), checkbox class(chkSelectAll) is selected checked true. im confuse...please help
DamithSL 16-May-14 12:12pm    
check my update
Ade.Ruyani.Z 16-May-14 12:17pm    
thankyou damithsl, this is work without problem. your great n help me.
DamithSL 16-May-14 12:20pm    
You're welcome!

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