Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

checkbox 'all'

Posted on 2010-08-30
8
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 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 82

Expert Comment

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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

636 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