How to Select CheckBox using JavaScript

Source: Internet
Author: User

It is rare to use JavaScript to select all items in the CheckBox. This is a gift carefully prepared by the author. The code is highly authenticated and well understood. I hope it will help you.

JavaScript implements the CheckBox method to select all items:

 
 
  1. <Html> 
  2. <Head> 
  3. <Title> 
  4. Title> 
  5. <Body> 
  6. <Form Name="Form1"> 
  7. <Input Name="Allbox" Type="Checkbox" OnClick="TA ();" Title="Select or clear all selected projects" Tabindex="105">Select or clear all selected projects
  8. <Br/> 
  9. <Span Class="X"> 
  10. <Input Type="Checkbox" Name="ABC" Class="" OnClick="TTA (this );">Qingming Festival Span> 
  11. <Br/> 
  12. <Span Class="X"> 
  13. <Input Type="Checkbox" Name="DEF" Class="" OnClick="TTA (this );">Changjiang Span> 
  14. <Br/> 
  15. <Span Class="X"> 
  16. <Input Type="Checkbox" Name="GHL" Class="" OnClick="TTA (this );">Hey, test Span> 
  17. <Br/> 
  18. Form> 
  19. <Script Language="Javascript"> 
  20. VarFrm=Document. Form1;
  21. Script> 
  22. <Script Language="JavaScript"> 
  23. Function TA (isO, noHL ){
  24. VarTrk=0;
  25. For (varI=0; I<Frm. elements. length; I ++ ){
  26. VarE=Frm. Elements [I];
  27. If (e. name! = 'Allbox ')&&(E. type= 'Checkbox ')){
  28. E. checked=Frm. Allbox. checked;
  29. If (frm. allbox. checked ){
  30. //E. parentElement. className="H"; //. ParentElement is not compatible with firefox
  31. E. parentNode. className="H"; // Call to set the parent node element class to change the background color
  32. } Else {
  33. E. parentNode. className="X"; // Call to set the parent node element class to change the background color
  34. }
  35. }
  36. }
  37. }
  38. Function TTA (CB, noHL ){
  39. If (CB. checked ){
  40. CB. parentNode. className="H";
  41. } Else {
  42. CB. parentNode. className="X";
  43. }
  44. VarTB=TO= 0;
  45. For (varI=0; I<Frm. elements. length; I ++ ){
  46. VarE=Frm. Elements [I];
  47. If (e. name! = 'Allbox ')&&(E. type= 'Checkbox ')){
  48. TB ++;
  49. If (e. checked)
  50. TO ++;
  51. }
  52. }
  53.  Frm. allbox. checked= (TO= TB )? True: false;
  54. // If not all items are selected, allbox settings are not selected.
  55. }
  56. Script> 
  57. <Style> 
  58. . H {
  59. Background: # ADD8E6
  60. }
  61. . X {
  62. Background: # FFFFFF
  63. }
  64. Style> 
  65. Body> Html> 

The above is a brief introduction to the methods used to select all items in the CheckBox in JavaScript.

  1. What is JSON? Data format prepared for JavaScript
  2. 10 most commonly used JavaScript udfs
  3. Some extended thoughts on loading JavaScript events
  4. Summary of JavaScript usage: From BOM and DOM
  5. How ExtJS works on Android Simulators

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.