The DOM check box in javascript selects the usage instance

  • 2020-06-12 08:31:38
  • OfStack

This article illustrates the use of DOM check box selection in javascript. Share to everybody for everybody reference. The details are as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Check boxes are all clear and unchecked </title>
<script type="text/javascript">
// Gets an array of all check box objects 
function GetAllCheckBox() {
  var div = document.getElementById("Balls");
  var inputs = div.getElementsByTagName("input");
  // Defines an array of check boxes to return 
  var checkboxs = new Array();
  var nIndex = 0;
  for (var i = 0; i < inputs.length; i++) {
 // through type Whether it is checkbox To determine the checkbox 
 if (inputs[i].type == "checkbox") {
   checkboxs[nIndex] = inputs[i];
   nIndex++;
 }
  }
  return checkboxs;
}
// Future generations 
function selAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < checkboxs.length; i++) {
 checkboxs[i].checked = true;
  }
}
// All clear 
function clearAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < checkboxs.length; i++) {
 checkboxs[i].checked = false;
  }
}
// The selected 
function reverseAll() {
  var checkboxs = GetAllCheckBox();
  for (var i = 0; i < checkboxs.length; i++) {
 if (checkboxs[i].checked == true) {
   checkboxs[i].checked = false;
 }
 else {
   checkboxs[i].checked = true;
 }
  }
}
</script>
</head>
<body>
<div id="Balls">
<input type="checkbox" id="c1" /><label for="c1"> football </label>
<input type="checkbox" id="c2" /><label for="c2"> billiards </label>
<input type="checkbox" id="c3" /><label for="c3"> Table tennis </label>
<br />
<input type="button" value=" Future generations " onclick="selAll()" />
<input type="button" value=" All clear " onclick="clearAll()" />
<input type="button" value=" The selected " onclick="reverseAll()" />
</div>
</body>
</html>

Hopefully, this article has been helpful in your javascript programming.


Related articles: