버튼 클릭 한 번으로 여러 체크박스를 모두 선택하거나 해제하려면 id 속성과 함께 jQuery()를 사용하면 됩니다. 버튼의 토글 상태에 따라 toggleClass()와 prop() 메서드를 활용해 체크박스의 선택 여부를 제어하는 방식입니다. 다음은 전체 예제 코드입니다.
예제
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/4.7.0/css/font-awesome.min.css">
<style>
.changeColor {
color: red
};
</style>
</head>
<body>
<input type="button" id="selectDemo" value="Want To Select All
Values" />
<table>
<tr>
<td>
<input type="checkbox" id="CheckBoxId1"
class="isSelected" /> Javascript
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckBoxId2"
class="isSelected" /> MySQL
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckBoxId3"
class="isSelected" /> MongoDB
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckBoxId4"
class="isSelected" /> Python
</td>
</tr>
</table>
<script>
jQuery("#selectDemo").click(function () {
jQuery(this).toggleClass("changeColor");
if (jQuery(this).hasClass("changeColor")) {
jQuery(".isSelected").prop("checked", true);
jQuery(this).val("Want To UnSelect All Values");
} else {
jQuery(this).removeClass("changeColor");
jQuery(".isSelected").prop("checked", false);
jQuery(this).val("Want To Select All Values");
}
});
</script>
</body>
</html>
위 프로그램을 실행하려면 파일 이름을 "anyName.html"(index.html)로 저장한 뒤 해당 파일을 마우스 오른쪽 버튼으로 클릭하세요. VS Code 편집기에서 "Open with Live Server" 옵션을 선택하면 브라우저에서 바로 결과를 확인할 수 있습니다.
출력 결과
위 코드를 실행하면 다음과 같은 화면이 표시됩니다.

이제 "Want To Select All Values"(모든 값 선택) 버튼을 클릭해 보겠습니다. 버튼을 클릭하면 버튼 색상이 빨간색으로 변경되면서 모든 체크박스가 한꺼번에 선택됩니다.

이때 버튼의 문구는 "Want To UnSelect All Values"(모든 값 선택 해제)로 바뀝니다. 이 상태에서 버튼을 다시 한 번 클릭하면 모든 체크박스가 해제됩니다.
