SoFunction
Updated on 2025-03-01

DOM check box selection usage instance in javascript

This article describes the usage of DOM check box selection in JavaScript. Share it for your reference. The details are as follows:

<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Select all check boxes and select all clear and reverse</title>
<script type="text/javascript">
//Get all checkbox object arrayfunction GetAllCheckBox() {
  var div = ("Balls");
  var inputs = ("input");
  //Define the check box array to return  var checkboxs = new Array();
  var nIndex = 0;
  for (var i = 0; i < ; i++) {
 //Judge the checkbox by whether the type is checkbox or not if (inputs[i].type == "checkbox") {
   checkboxs[nIndex] = inputs[i];
   nIndex++;
 }
  }
  return checkboxs;
}
//Select allfunction selAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < ; i++) {
 checkboxs[i].checked = true;
  }
}
//Full Qingfunction clearAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < ; i++) {
 checkboxs[i].checked = false;
  }
}
//Reverse selectionfunction reverseAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < ; i++) {
 if (checkboxs[i].checked == true) {
   checkboxs[i].checked = false;
 }
 else {
   checkboxs[i].checked = true;
 }
  }
}
</script>
</head>
<body>
<div >
<input type="checkbox"  /><label for="c1">football</label>
<input type="checkbox"  /><label for="c2">billiards</label>
<input type="checkbox"  /><label for="c3">table tennis</label>
<br />
<input type="button" value="Select All" onclick="selAll()" />
<input type="button" value="Full Qing" onclick="clearAll()" />
<input type="button" value="Anti-select" onclick="reverseAll()" />
</div>
</body>
</html>

I hope this article will be helpful to everyone's JavaScript programming.