[jquery] How do I check/uncheck all checkboxes with a button using jQuery?

Try below code to check/uncheck all checkboxes

_x000D_
_x000D_
jQuery(document).ready(function() {_x000D_
    $("#check_uncheck").change(function() {_x000D_
        if ($("#check_uncheck:checked").length) {_x000D_
            $(".checkboxes input:checkbox").prop("checked", true);_x000D_
        } else {_x000D_
            $(".checkboxes input:checkbox").prop("checked", false);_x000D_
        }_x000D_
    })_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
<input type="checkbox" name="check_uncheck" id="check_uncheck" /> Check All/Uncheck All_x000D_
<br/>_x000D_
<br/>_x000D_
<div class="checkboxes">_x000D_
    <input type="checkbox" name="check" id="check" /> Check Box 1_x000D_
    <br/>_x000D_
    <input type="checkbox" name="check" id="check" /> Check Box 2_x000D_
    <br/>_x000D_
    <input type="checkbox" name="check" id="check" /> Check Box 3_x000D_
    <br/>_x000D_
    <input type="checkbox" name="check" id="check" /> Check Box 4_x000D_
</div>
_x000D_
_x000D_
_x000D_

Try this Demo Link

There is also another short way to do this, checkout below example. In this example check/uncheck all checkbox is checked or not if checked then all wiil be checked and if not all will be uncheck

_x000D_
_x000D_
$("#check_uncheck").change(function() {_x000D_
    $(".checkboxes input:checkbox").prop("checked",$(this).is(':checked'));_x000D_
})
_x000D_
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
    <input type="checkbox" name="check_uncheck" id="check_uncheck" /> Check All/Uncheck All_x000D_
    <br/>_x000D_
    <br/>_x000D_
    <div class="checkboxes">_x000D_
        <input type="checkbox" name="check" id="check" /> Check Box 1_x000D_
        <br/>_x000D_
        <input type="checkbox" name="check" id="check" /> Check Box 2_x000D_
        <br/>_x000D_
        <input type="checkbox" name="check" id="check" /> Check Box 3_x000D_
        <br/>_x000D_
        <input type="checkbox" name="check" id="check" /> Check Box 4_x000D_
    </div>
_x000D_
_x000D_
_x000D_