Question : JavaScript: Alert when checkbox value changed

I want to alert a message when the value of a checkbox is changed.
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:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
</head>
<body>

<div>
<input type="checkbox" name="a" value="1" />
<input type="checkbox" name="b" value="2" />
<input type="checkbox" name="c" value="3" />
</div>

<script type="text/javascript">
/*<![CDATA[*/


 var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
 var myCheckboxes = []; //will contain all checkboxes  
 var myCheckboxesChecked = []; //will contain all checked checkboxes  
 var ci=0;
 for (var i = 0; i < inputs.length; i++) {  
  if (inputs[i].type == "checkbox") {  
  ci++;
    myCheckboxes.push(inputs[i]);  

    inputs[i].onchange="alert('A checkbox value was changed')";

  }  
 }  


/*]]>*/
</script>


</body>
</html>
Related Solutions: JavaScript: i++ in a loop

Answer : JavaScript: Alert when checkbox value changed

better?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
<html>
<head>
<title>Example</title>
</head>
<body>

<div>
<input type="checkbox" name="a" value="1" />
<input type="checkbox" name="b" value="2" />
<input type="checkbox" name="c" value="3" />
</div>

<script type="text/javascript">
 var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
 for (var i = 0; i < inputs.length; i++) {  
  if (inputs[i].type == 'checkbox') {  
    inputs[i].onclick=function() { alert('A checkbox value was changed');
    }
  }  
 }  
</script>

</body>
</html>
Random Solutions  
 
programming4us programming4us