[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

checkbox 'all'

Posted on 2010-08-30
8
Medium Priority
?
442 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

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 83

Accepted Solution

by:
leakim971 earned 2000 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 111

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 83

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 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