1

i need help, anybody help me for checkbox using javascript? this is my script:

<!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...

2
  • jsfiddle.net/w8z9y this code works like a charm, what is the problem then? Are you sure this works: @Scripts.Render("~/bundles/jquery")??? Commented May 16, 2014 at 15:45
  • i'm using sample with mvc so i render jquery script, 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, Commented May 16, 2014 at 15:51

1 Answer 1

1

I misunderstood your question, you need to check if the checkboxes changed and check if all changed.

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

Here is the fiddle: http://jsfiddle.net/w8z9y/1/

Sign up to request clarification or add additional context in comments.

1 Comment

i also misunedrstand OP question but you have posted right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.