Select a usage instance for the DOM check box in javascript

Source: Internet
Author: User

Select a usage instance for the DOM check box in javascript

This example describes how to select the DOM check box in javascript. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> check box select all clear and reselect </title>

<Script type = "text/javascript">

// Retrieve the array of all check box objects

Function GetAllCheckBox (){

Var div = document. getElementById ("bils ");

Var inputs = div. getElementsByTagName ("input ");

// Define an array of check boxes to return

Var checkboxs = new Array ();

Var nIndex = 0;

For (var I = 0; I <inputs. length; I ++ ){

// Determine the check box by checking whether the type is checkbox

If (inputs [I]. type = "checkbox "){

Checkboxs [nIndex] = inputs [I];

NIndex ++;

}

}

Return checkboxs;

}

// Select all

Function selAll (){

Var checkboxs = GetAllCheckBox ();

For (var I = 0; I <checkboxs. length; I ++ ){

Checkboxs [I]. checked = true;

}

}

// Clear all

Function clearAll (){

Var checkboxs = GetAllCheckBox ();

For (var I = 0; I <checkboxs. length; I ++ ){

Checkboxs [I]. checked = false;

}

}

// Invert Selection

Function reverseAll (){

Var checkboxs = GetAllCheckBox ();

For (var I = 0; I <checkboxs. length; I ++ ){

If (checkboxs [I]. checked = true ){

Checkboxs [I]. checked = false;

}

Else {

Checkboxs [I]. checked = true;

}

}

}

</Script>

</Head>

<Body>

<Div id = "bils">

<Input type = "checkbox" id = "c1"/> <label for = "c1"> football </label>

<Input type = "checkbox" id = "c2"/> <label for = "c2"> billiards </label>

<Input type = "checkbox" id = "c3"/> <label for = "c3"> Table Tennis </label>

<Br/>

<Input type = "button" value = "select all" onclick = "selAll ()"/>

<Input type = "button" value = "" onclick = "clearAll ()"/>

<Input type = "button" value = "invert" onclick = "reverseAll ()"/>

</Div>

</Body>

</Html>

I hope this article will help you design javascript programs.

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.