Solved

checkbox 'all'

Posted on 2010-08-30
8
425 Views
Last Modified: 2012-05-10
5 checkboxes
and one checkbox 'all'

if I check one checkbox 'all'
then all the checkboxes get checked

0
Comment
Question by:rgb192
8 Comments
 
LVL 10

Expert Comment

by:ALaRiva
ID: 33562033
What is your question?
0
 
LVL 8

Expert Comment

by:Pramod Bugudai
ID: 33562083
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33562169
Try this function

for(i=0;i<document.getElementsByTagName("input").length;i++)
{
   if(document.getElementsByTagName("input")[i].type == "checkbox")
  {
    document.getElementsByTagName("input")[i].checked = true;
  }
}

Hope this helps
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 82

Expert Comment

by:leakim971
ID: 33563403
You may use this :

      function setCheckboxes(groupName, state) {
            var checkboxes = document.getElementsByName(groupName);
            for(var i=0;i
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function setCheckboxes(groupName, state) {
		var checkboxes = document.getElementsByName(groupName);
		for(var i=0;i<checkboxes.length;i++) {
			checkboxes[i].checked = state;
		}
	}
</script>
</head>
<body>
<input type="checkbox" id="all" onclick="setCheckboxes('group1', this.checked);" />Check all<br />
<input type="checkbox" id="cb1" name="group1" />One<br />
<input type="checkbox" id="cb2" name="group1" />Two<br />
<input type="checkbox" id="cb3" name="group1" />Three<br />
<input type="checkbox" id="cb4" name="group1" />Four<br />
<input type="checkbox" id="cb5" name="group1" />Five<br />
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33563412
If you cannot use the name of the checkboxes, we may use a class for each one (again, not for the first one which check or uncheck all) : group1

      function setCheckboxes(groupName, state) {
            var inputs = document.getElementsByTagName("input");
            for(var i=0;i<inputs.length;i++) {
                  if(inputs[i].getAttribute("class") && inputs[i].getAttribute("class").indexOf("group1")>=0) {
                        inputs[i].checked = state;
                  }
            }
      }

test page :



<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function setCheckboxes(groupName, state) {
		var inputs = document.getElementsByTagName("input");
		for(var i=0;i<inputs.length;i++) {
			if(inputs[i].getAttribute("class") && inputs[i].getAttribute("class").indexOf("group1")>=0) {
				inputs[i].checked = state;
			}
		}
	}
</script>
</head>
<body>
<input type="checkbox" id="all" onclick="setCheckboxes('group1', this.checked);" />Check all<br />
<input type="checkbox" id="cb1" name="cb1" class="group1" />One<br />
<input type="checkbox" id="cb2" name="cb2" class="group1" />Two<br />
<input type="checkbox" id="cb3" name="cb3" class="group1" />Three<br />
<input type="checkbox" id="cb4" name="cb4" class="group1" />Four<br />
<input type="checkbox" id="cb5" name="cb5" class="group1" />Five<br />
</body>
</html>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33567092
Might be possible in PHP, too.  Just test the checkbox named "all" and use its state to set the other checkboxes, or use something like this:

if ($checkboxOne or $checkboxAll) /* DO SOMETHING */
if ($checkboxTwo or $checkboxAll) /* DO SOMETHING ELSE,,, */
0
 

Author Closing Comment

by:rgb192
ID: 33570031
thanks

answered the question in javascript
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33570090
You're welcome! Thanks for the points!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Security 29 48
Button function on table is in trouble 3 21
Running a javascript project in WebStorm 2 25
Test if unique ID is in log file 5 21
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question