Home > Web Front-end > JS Tutorial > How to implement 'select all' and 'select none' functions in JavaScript? (code example)

How to implement 'select all' and 'select none' functions in JavaScript? (code example)

青灯夜游
Release: 2020-06-16 10:27:32
Original
3694 people have browsed it

This article will introduce to you how to use JavaScript to implement the "select all" and "unselect all" functions. The article introduces it in detail through sample code. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

The code is as follows

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<script>

  function clickon() {
    // 获取到body中所有checkbox
    var checkbox = document.querySelectorAll("input[type=&#39;checkbox&#39;]");

    for (var i = 0; i < checkbox.length; i++) {
      checkbox[i].checked = true;
    }
  }

  function unclick() {
    var checkbox = document.querySelectorAll("input[type=&#39;checkbox&#39;]");

    for (var i = 0; i < checkbox.length; i++) {
      checkbox[i].checked = false;
    }
  }

</script>
<body>

<form>
  <input type="checkbox">吃
  <input type="checkbox">喝
  <input type="checkbox">拉
  <input type="checkbox">撒
  <input type="button" value="全选" onclick="clickon()">
  <input type="button" value="全不选" onclick="unclick()">
</form>

</body>
</html>
Copy after login

Effect

##For more jQuery and Javascript special effects, it is recommended to visit:

js special effects collection!

The above is the detailed content of How to implement 'select all' and 'select none' functions in JavaScript? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template