JavaScript implements checkbox selection function

JavaScript implements checkbox selection function

This article example shares the specific code of JavaScript to achieve the selection of all checkboxes for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 300px;
            border-spacing: 0;
            /* text-align: center; */
            margin: 100px auto;
            border-collapse: collapse;
        }
        
        table tr:nth-child(n+2)>td {
            text-align: left;
            background-color: rgb(250, 245, 245);
            color: dimgray;
            font-size: 14px;
        }
        
        table tr:nth-child(1) {
            background-color: skyblue;
            color: white;
        }
        
        th,
        td {
            padding: 10px;
            border: 0.5px solid gray;
        }
    </style>
</head>

<body>
    <table>
        <tr>
            <th><input type="checkbox" name="" id="all"></th>
            <th>Products</th>
            <th>Price</th>
        </tr>
        <tr>
            <td><input type="checkbox" name="" id="ip8"></td>
            <td>iPhone8</td>
            <td>8000</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" id="pro"></td>
            iPad Pro
            <td>5000</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" id="air"></td>
            iPad Air
            <td>2000</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" id="watch"></td>
            <td>Apple Watch</td>
            <td>2000</td>
        </tr>
    </table>

    <script>
        var all = document.getElementById('all');
        var items = document.querySelectorAll('tr>td>input');
        all.onclick = function() {
            for (var i = 0; i < items.length; i++) {
                items[i].checked = this.checked;
            }
        }


        for (var i = 0; i < items.length; i++) {
            items[i].onclick = function() {
                var flag_all = 1;
                for (var j = 0; j < items.length; j++) {
                    if (items[j].checked == 0) {
                        flag_all = 0;
                        all.checked = flag_all;
                        break;
                    }
                }
                all.checked = flag_all;
            }
        }
    </script>
</body>

</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implements the functions of selecting all and deleting batches of check boxes
  • JS implements the function of selecting all, unselecting or unselecting all checkboxes
  • JS implements the function of selecting all and deselecting all in CheckBox
  • How to select all/unselect all checkboxes using js and jQuery
  • js realizes the effect of selecting and deselecting all checkboxes
  • js html css to achieve checkbox all selection and inverse selection
  • Detailed explanation of javascript to implement checkbox select all and reverse select events
  • javascript checkbox selection/select all special effects
  • How to implement the checkbox select all function in JS
  • Select and deselect all checkboxes based on JavaScript

<<:  More Ways to Use Angle Brackets in Bash

>>:  2017 latest version of windows installation mysql tutorial

Recommend

This article will help you understand JavaScript variables and data types

Table of contents Preface: Kind tips: variable 1....

A brief analysis of the differences between undo, redo and binlog in MySQL

Table of contents Preface 【undo log】 【redo log】 【...

Detailed explanation of ES6 Promise usage

Table of contents What is a Promise? Usage of rej...

Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA

1. Add the plug-in and add the following configur...

Nodejs plug-in and usage summary

The operating environment of this tutorial: Windo...

Detailed explanation of the difference between JavaScript onclick and click

Table of contents Why is addEventListener needed?...

How to solve the problem that MySQL cannot start because it cannot create PID

Problem Description The MySQL startup error messa...

A brief discussion on the correct approach to MySQL table space recovery

Table of contents Preliminary Notes Problem Repro...

An article to understand operators in ECMAScript

Table of contents Unary Operators Boolean Operato...

A brief talk about Mysql index and redis jump table

summary During the interview, when discussing abo...

Summary of Common Terms in CSS (Cascading Style Sheet)

If you use CSS don't forget to write DOCTYPE, ...

DHCP Configuration Tutorial in CentOS7 Environment

Table of contents Configuration command steps in ...

JS practical object-oriented snake game example

Table of contents think 1. Greedy Snake Effect Pi...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...