Home>Article>Web Front-end> How to select all in javascript

How to select all in javascript

Patricia Arquette
Patricia Arquette Original
2022-01-18 11:26:17 6424browse

Javascript method to implement all-select: 1. Create a form through HTML and CSS; 2. Use DOM methods to obtain the "input" elements for all-select and single-select respectively; 3. Set a variable isAllChecked; 4. Assign the value of isAllChecked to the Select All button.

How to select all in javascript

The operating environment of this article: Windows 7 system, javascript version 1.8.5, Dell G3 computer.

javascript How to select all?

Use JS to implement full selection and inverse selection in the form

This is also an exercise, which is to select all in the general e-commerce shopping cart, and add an inverse selection. (It seems that there are not many shopping carts with the inverse selection function, but Windows Explorer has the inverse selection function)

How to select all in javascript

Let’s start with selecting all:

  • When all 4 rows of products are checked, "Select All" is automatically checked; when any product is not checked, "Select All" is automatically canceled.

  • When all 4 rows of products are not checked, check "Select All" to make all 4 rows of products checked;

  • When "Select All" is checked, uncheck "Select All" and all products will be unchecked.

After sorting out the requirements, we first use HTML and CSS to draw this form. The code is as follows:

   全选2  
商品 价格
iPhone X 8000
iPad pro 5000
iPad air 2000
Apple watch 2000

Here I have put the function of the mouse click event After naming it, call it "funcAll()" for all selection, "funcOne()" for single selection, and "funcBackward()" for reverse selection. The CSS part mainly adds borders to the form, and at the same time blends the borders at the junction, which is basically the simplest look.

Let’s talk about requirements 2 and 3 first. The logic is relatively simple:

Use the DOM method to obtain the "input" elements for full selection and single selection respectively. If there is only one for all selection, use it directly. Just getElementById() is enough (the id has been set in advance), and there are four radio selections. Use getElementByClassName() to get it (the class has been set in advance). Of course, what you get is an array.

Assign the obtained elements to variables, then use a for loop to traverse the array, and assign the checked attribute of the variable that selects all to the checked attribute of each single-selected variable.

At this point, 2 and 3 have been solved at the same time. The code is as follows:

function funcAll(){ var selectAll = document.getElementById('selectAll'); var selectOnes = document.getElementsByClassName('selectOne'); for (var i = 0; i 

Next, let’s talk about item 1: “When all 4 rows of products are checked, “Select All” automatically is checked; when any product is not checked, "Select All" is automatically canceled."

Here, the status of "Select All" is similar to monitoring all other buttons. Once there is a change, it will be automatically changes. So this logic needs to be written into the mouse click event of the radio button of each row of products.

I set up a variable isAllChecked as a bridge. The initial state defines isAllChecked as true, and loops through each radio button. Once it is detected that a radio button is not checked, set isAllChecked to false. Break out of the loop and assign the value of isAllChecked to the Select All button.

In this way, as long as one radio button is not selected, all selections will be turned into an unselected state. , the code is as follows:

function funcOne(){ var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍 var selectOnes = document.getElementsByClassName('selectOne'); var isAllChecked = true; //定义一个变量作为桥梁来控制全选按钮 for (var i = 0; i 

Let’s talk about inverse selection

  1. Click the inverse selection button, and the status of all radio button buttons will be inverted: the selected ones become unselected, and the unselected ones Change the selection to
  2. and click Inverse Selection. The result will still make the selection consistent with the previous logic.

Item 1 is actually very easy to implement. When you click the invert button, cycle through the 4 radio buttons and invert the checked attribute corresponding to each element.

However, the select-all listener for all single items we implemented earlier is actually implemented in the click event of each radio button. That is to say, if we do not change the single item by clicking the radio button, The state of the selection button, Select All, is actually not monitored globally.

So, we can write the monitoring code into the reverse selection mouse click function. The final code is as follows:

 var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍 var selectOnes = document.getElementsByClassName('selectOne'); for (var i = 0; i 

The final effect is as follows:

How to select all in javascript

Recommended study: "js Basic Tutorial"

The above is the detailed content of How to select all in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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