Solved

checkbox 'all'

Posted on 2010-08-30
8
408 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 30

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now