Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 04:09 PM   PM User | #1
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
Fire group of checkboxes at once

I have a group of checkboxes that can all be checked at once via the first checkbox which toggles all. However, each of those checkboxes include an onclick-this function (also tried with onchange), but it does not fire at the toggling. How to make this happen?

Code:
<script>
function checkall(n) {
 var togglebox = document.getElementsByName('toggle')[n];
 var boxes = document.getElementById('Categories').getElementsByTagName('div')[n].getElementsByTagName('input');

 for (var i = 0; i < boxes.length; i++) {
  if (togglebox.checked == true) {
   boxes[i].checked = true;
  }
  else {
   boxes[i].checked = false;
  }
 }
}

function category(o) {
 if (o.checked == true) {
  alert (o.value);
 }
}
</script>

<div id="Categories">
 <div>
  <input type="checkbox" name="toggle" onclick="checkall(0);">Sections
  <input type="checkbox" name="category" value="1" onclick="category(this)">Section 1
  <input type="checkbox" name="category" value="2" onclick="category(this)">Section 2
  <input type="checkbox" name="category" value="3" onclick="category(this)">Section 3
 </div>
</div>
Much appreciated! Thanks!

Last edited by snakehill; 02-13-2013 at 03:31 PM..
snakehill is offline   Reply With Quote
Old 02-12-2013, 04:15 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Adapt this to your needs:-

Code:
<div id="mydiv1">
A<input type="checkbox"><br>
B<input type="checkbox"><br>
C<input type="checkbox"><br>
D<input type="checkbox"><br>
E<input type="checkbox"><br> <br>
</div>

<script type="text/javascript">

function checkByParent(aId, aChecked) {
var collection = document.getElementById(aId).getElementsByTagName("input");
for (var x=0; x<collection.length; x++) {
if (collection[x].type.toLowerCase() == "checkbox") {
collection[x].checked = aChecked;
}
}
}

function toggle(aId) {
var collection = document.getElementById(aId).getElementsByTagName("input");
for (var x=0; x<collection.length; x++) {
if (collection[x].type.toLowerCase() == "checkbox") {
if (collection[x].checked) {
collection[x].checked = false;
}
else {collection[x].checked = true}
}
}
}

//checkByParent("mydiv1", true);  // check all boxes initially

</script>

<input type="button" value="Check All" onclick="checkByParent('mydiv1', true)">
<input type="button" value="Uncheck All" onclick="checkByParent('mydiv1', false)">
<input type="button" value="Toggle All" onclick="toggle('mydiv1')">

“I don't pretend we have all the answers. But the questions are certainly worth thinking about..” - Arthur C. Clarke quotes (English Writer of science fiction, b.1917
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-12-2013, 04:26 PM   PM User | #3
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
But when adding any onclick or onchange functions to the mydiv1 checkboxes, these don't fire either, they merely get checked but any of the functions they carry don't run.
snakehill is offline   Reply With Quote
Old 02-12-2013, 04:51 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I am not sure that I understand that. onclick only works onclick (an actual click), not if the checkmark is changed programatically. onchange does not apply to checkboxes. I think you are trying to do something which cannot be achieved.

You will have to use a button and a separate function to fire all the currently checked checkboxes if that is what you want.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-12-2013 at 04:57 PM..
Philip M is offline   Reply With Quote
Old 02-12-2013, 05:12 PM   PM User | #5
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
Hmm.. any way you recommend for doing that in case of the code in the main post? You won't have to code it for me but I think I need a push in the right direction as for that.

Thank you!!
snakehill is offline   Reply With Quote
Old 02-12-2013, 05:44 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by snakehill View Post
Hmm.. any way you recommend for doing that in case of the code in the main post? You won't have to code it for me but I think I need a push in the right direction as for that.

Thank you!!
As I say, it is not possible to to fire a function as though onclick when the state of the checkbox has been changed programatically.

You will have to use a button and a separate function to fire all the currently checked checkboxes if that is what you want.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-12-2013, 07:07 PM   PM User | #7
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by snakehill View Post
I have a group of checkboxes that can all be checked at once via the first checkbox which toggles all.
Due to an incorrect starting index, the code as shown does not do that.
Quote:
However, each of those checkboxes include an onclick-this function (also tried with onchange), but it does not fire at the toggling. How to make this happen?
Code:
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>TEST</title>
</head>
<body>

<script>

function checkall(n) 
{
 var togglebox = document.getElementsByName('toggle')[n];
 var boxes = document.getElementById('Categories').getElementsByTagName('div')[n].getElementsByTagName('input');

 for( var i = 1; boxes[ i ]; i++ ) 
   if( ( boxes[i].checked = togglebox.checked ) )
     boxes[i].onclick();
}

function category(o) {
 if (o.checked == true) {
  alert (o.value);
 }
}
</script>

<div id="Categories">
 <div>
  <input type="checkbox" name="toggle" onclick="checkall(0);">Sections
  <input type="checkbox" name="category" value="1" onclick="category(this)">Section 1
  <input type="checkbox" name="category" value="2" onclick="category(this)">Section 2
  <input type="checkbox" name="category" value="3" onclick="category(this)">Section 3
 </div>
</div>
</body>

</html>
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
snakehill (02-13-2013)
Old 02-12-2013, 09:03 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
The onclick for the checkbox that checks all the other checkboxes simply needs to call all the functions that the other onclicks call when the checkboxes are clicked individually.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 02-13-2013, 03:32 PM   PM User | #9
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
Brilliant! Thanks a whole bunch!!!
snakehill is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:00 AM.


Advertisement
Log in to turn off these ads.