JavaScript implementation checkbox selection and cancellation of all selected code _ form effects

Source: Internet
Author: User
This article describes the method, the trigger condition is independent, you can select or deselect all the designated name checkbox, the same page can have more than one group for the checkbox, the function is sound, general strong.
Effect Chart:

To run the View effect:
<body> <script type= "Text/javascript" > Function checkall (name) {var el = document.getelementsbytagname (' I Nput '); var len = el.length; for (var i=0; i<len; i++) {if (el[i].type== "checkbox") && (El[i].name==name)) {el[i].checked = true; }} function ClearAll (name) {var el = document.getelementsbytagname (' input '); var len = el.length; for (var i=0; i<len; i++) {if (el[i].type== "checkbox") && (El[i].name==name)) {el[i].checked = false; }} </script> <input type= "checkbox" name= "Test" value= "onclick=" if (this.checked==true) {checkall (' test ') ); else {clearall (' Test ');} '/> Alphabetical switch <input type= checkbox ' name= ' Test ' value= ' a '/> A <input type= ' check Box "name=" Test "value=" B "/> b <input type=" checkbox "name=" Test "value=" C "/> C <input type=" checkbox "Nam E= "Test" value= "D"/> D <input type= "checkbox" name= "Test" value= "E"/> e <input type= "checkbox" name= "Test" Value= "F"/> F <input type= "checkbox" name= "Test" value= "G"/> g <br> <input type= "checkbox" Name= "num" value= "" Oncl Ick= ' if (this.checked==true) {checkall (' num ');} else {clearall (' num ');} "/> Digital all switches <input type=" checkbox "Name=" n Um "value=" 1 "/> 1 <input type=" checkbox "Name=" num "value=" 2 "/> 2 <input type=" checkbox "Name=" num "value= "3"/> 3 <br><br> <input type= "button" value= "Select All Letters" onclick= "Checkall (' Test ')"/> <input type = "button" value= "clears the selected letter" onclick= "ClearAll (' Test ')"/> <br><br> <input type= "button" value= " Select All digital "onclick=" checkall (' num ') "/> <input type=" button "value=" to empty the selected number "onclick=" clearall (' num ')/> </ Body> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

JS function
Copy Code code as follows:

<script type= "Text/javascript" >
function Checkall (name) {
var el = document.getelementsbytagname (' input ');
var len = el.length;
for (var i=0; i<len; i++) {
if ((el[i].type== "checkbox") && (El[i].name==name)) {
El[i].checked = true;
}
}
}
function ClearAll (name) {
var el = document.getelementsbytagname (' input ');
var len = el.length;
for (var i=0; i<len; i++) {
if ((el[i].type== "checkbox") && (El[i].name==name)) {
el[i].checked = false;
}
}
}
</script>

Html
Copy Code code as follows:

<input type= "checkbox" name= "Test" value= "onclick=" if (this.checked==true) {checkall (' Test ');} else {ClearAll (' Test '); } "/> Letter all switch
<input type= "checkbox" name= "Test" value= "a"/> A
<input type= "checkbox" name= "Test" value= "B"/> b
<input type= "checkbox" name= "Test" value= "C"/> C
<input type= "checkbox" name= "Test" value= "D"/> D
<input type= "checkbox" name= "Test" value= "E"/> E
<input type= "checkbox" name= "Test" value= "F"/> F
<input type= "checkbox" name= "Test" value= "G"/> g
<br/>
<input type= "checkbox" Name= "num" value= "onclick=" if (this.checked==true) {checkall (' num ');} else {clearall (' num ' ); } "/> Digital switch <input type=" checkbox "Name=" num "value=" 1 "/> 1
<input type= "checkbox" Name= "num" value= "2"/> 2
<input type= "checkbox" Name= "num" value= "3"/> 3
<br/>
<br/>
<input type= "button" value= "Select all the Letters" onclick= "Checkall (' Test ')" "/> <input type=" button "value=" Empty Selected letters " onclick= "ClearAll (' Test ')"/> <br/>
<br/>
<input type= "button" value= "Select all Numbers" onclick= "checkall (' num ')"/> <input "button" type= "Empty selected number" onclick= "ClearAll (' num ')"/>
Related Article

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.