[JavaScript] select all or invert check boxes, determine which check boxes are selected, and select all javascript check boxes.

Source: Internet
Author: User

[JavaScript] select all or invert check boxes, determine which check boxes are selected, and select all javascript check boxes.

This function is not difficult to select and select all check boxes and determine which check boxes are selected,

The use of document. getElementsByName ("xxx"); can be easily implemented. Note that the returned value is a node array.

However, this feature is very considerate to users. The following example illustrates the problem.

The first is the following layout:


The HTML code is as follows, which is very simple. Three buttons are used to set the javascript functions corresponding to the onclick event.

Then there are four check boxes. Pay attention to setting the unified name value to form a node array. So that it can be obtained by the subsequent document. getElementsByName method.

<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd"> The following is a javascript processing function. To achieve the following results, the processing script is as follows:


<Script type = "text/javascript"> // Where is the check box selected? Traverse the check box array and use value to retrieve its value function selected () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); var str = "the selected check boxes are:"; for (var I = 0; I <checkBoxArr. length; I ++) {if (checkBoxArr [I]. checked) {str + = checkBoxArr [I]. value + "," ;}} alert (str) ;}// select all. traverse the check box array. All checked values are true function selectAll () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); for (var I = 0; I <checkBoxArr. lengt H; I ++) {checkBoxArr [I]. checked = "true" ;}/// returns the check box array. All checked values are reversed. Function selectReserve () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); for (var I = 0; I <checkBoxArr. length; I ++) {checkBoxArr [I]. checked = (! CheckBoxArr [I]. checked) ;}</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.