Solved

checkbox 'all'

Posted on 2010-08-30
8
422 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML5 frame 5 25
how can i check if the text is an Isogram ? 2 25
PHP curl issue VERBOSE output 18 35
mysql date time 14 28
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 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