How to operate the check box in HTML page

How to operate the check box in HTML page

Checkboxes are very common on web pages. Whether it is an e-commerce website or a platform, you will see checkboxes wherever there is a need to make a selection. Next, here are two small demos I wrote before, both about checkboxes, I hope they will be helpful to you.

The first one is about the operation of selecting and deselecting all checkboxes. Of course, I also added a small function in it, that is, when the checkboxes of the selected products or other things below are all selected, the select all button will also become selected; vice versa.

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            td{
                border: 1px solid black;
                text-align: center;
            }
            table{
                border: 1px solid black;
            }
            #opp{
                border-radius: 50%;
                width: 20px;
                height: 20px;
                border: 1px style #eee;
                outline-style: none;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <td><input id="all" type="checkbox">Select All</td>
                <td width="300px">Checkbox Select All Example</td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input id="opp" type="button">Invert selection</td>
                <td></td>
            </tr>
        </table>
        <script>
                var vll = document.getElementById("all");
                var vlist = document.getElementsByClassName("list");
                var vopp = document.getElementById("opp");
                vll.onclick=function(){ 
                    for(var i=0;i<vlist.length;i++){    
                        vlist[i].checked=vll.checked;
                    }
                }
                for( var i=0;i<vlist.length;i++){
                    vlist[i].onclick=function(){
                        if(this.checked==false){
                            vll.checked=false;  
                            }
                        else{
                            for(var i2=0;i2<vlist.length;i2++){ 
                                if(vlist[i2].checked==false){
                                    break;
                                }
                                if(i2>=vlist.length-1){
                                    vll.checked=true;   
                                }
                            }
                        }
                    }   
                }
                vopp.onclick=function(){
                    for(var i=0;i<vlist.length;i++){
                    vlist[i].checked=!vlist[i].checked;
                        if(vlist[i].checked==false){
                            vll.checked=false;
                        }
                    }
                }
        </script>
    </body>
</html>

The second one is to customize the checkbox style, which is to replace our checkbox with an image to add a cool effect; and here I completely use CSS3 writing, without involving JavaScript;

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box1{
                width: 1000px;
                height: 50px;
                position: relative;
            }
            input{
                width: 50px;
                height: 50px;
                opacity: 1;
                display: none;
            }
            input+label{
                display: block;
                width: 50px;
                height: 50px;
                background: url(img/2.png);
                background-size: 100%;
            }
            input:checked+label{
                background: url(img/1.PNG);
                background-size: 100%;
            }
        </style>
    </head>
    <body>
        <div class="box1">
            <input type="checkbox" name="" id="input1" value="" />
            <label for="input1"></label>
        </div>
        <div class="box2">
            <input type="checkbox" name="" id="input2" value="" />
            <label for="input2"></label>
        </div>
        <div class="box3">
            <input type="checkbox" name="" id="input3" value="" />
            <label for="input3"></label>
        </div>
    </body>
</html>

The above is the operation method of check boxes in HTML pages introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Introduction to common commands and shortcut keys in Linux

>>:  base target="" specifies the target of the base link to open the frame

Recommend

Summary of a CSS code that makes the entire site gray

In order to express the deep condolences of peopl...

MySQL 8.0 New Features: Hash Join

The MySQL development team officially released th...

Reasons why MySQL queries are slow

Table of contents 1. Where is the slowness? 2. Ha...

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration Ng...

Detailed explanation of how to mount remote file systems via SSH on Linux

Features of SSHFS: Based on FUSE (the best usersp...

HTML code to add quantity badge to message button

HTML code: <a onclick="goMessage();"...

ElementUI implements cascading selector

This article example shares the specific code of ...

Mysql: The user specified as a definer ('xxx@'%') does not exist solution

During the project optimization today, MySQL had ...

Let's talk in detail about how the NodeJS process exits

Table of contents Preface Active withdrawal Excep...

Web Design Tutorial (3): Design Steps and Thinking

<br />Previous tutorial: Web Design Tutorial...

MySQL 5.7.18 free installation version window configuration method

This is my first blog. It’s about when I started ...

Write a mysql data backup script using shell

Ideas It's actually very simple Write a shell...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

Detailed explanation of how to use Node.js to implement hot reload page

Preface Not long ago, I combined browser-sync+gul...

Learn MySQL index pushdown in five minutes

Table of contents Preface What is index pushdown?...