...

View Full Version : Checkbox Array - ability to check all



chellert
03-14-2012, 09:21 PM
Hello

I am trying to find a way to use the check all javascript code to select all my checkboxes within a while...loop.

Codes goes as follows:

within the header on the top of page 1:
<SCRIPT LANGUAGE="JavaScript">
function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}

function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
</script>

The code that displays the checkboxes, which is a page included onto page 1:
echo "<a class='comp' onclick='singleHideandShow({$row['pr_id']})' style='cursor:pointer'>{$row['propname']}</a><p>";
$propqry = mysql_query("SELECT * FROM users WHERE propid={$row['pr_id']}");
//using the hide and show id number, once clicked it will display the below contents
echo "<div id={$row['pr_id']} style='display:none;'>";
while($propf = mysql_fetch_assoc($propqry)) {
if($propf['uactive'] == "yes") {
$pactive = "active";
} else {
$pactive = "deactivated";
}
//displays the information from the DB with a checkbox
echo "<form name='myform' id='formmsg' method='post' action='profile.php?paction=edit&find=none' >";
echo "&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='check_list' value='{$propf['us_id']}'>&nbsp;&nbsp;<a href='profile.php?paction=edit&pid={$propf['us_id']}' /><img src='../images/secure/edit.png' name='Edit' border='0' /></a>&nbsp;<a href='profile.php?paction=delete&pid={$propf['us_id']}' /><img src='../images/secure/delete.png' name='Delete' border='0' /></a><label class='cuser'>&nbsp;&nbsp;{$propf['fname']} {$propf['lname']}, &nbsp;&nbsp; ({$propf['uname']}), &nbsp;&nbsp; {$pactive}</label><br>";

}
echo "</form>";
echo '<p><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.myform.check_list)"> <input type="button" name="Un_CheckAll" value="Uncheck All" onClick="UnCheckAll(document.myform.check_list)"> <p>';
echo "</div>";


I receive the following error message when I click on the Check All button.

Message: 'length' is null or not an object
Line: 15
Char: 13
Code: 0
URI: http://www.domainname.com/profile.php?paction=edit&find=none



Can anyone help me figure this out?

webdev1958
03-15-2012, 06:33 AM
You don't need 2 functions. A single function is all you need to check/uncheck all the checkboxes.

A quick and simple demo:



<body>
<input type="checkbox" name="chk1" value="val1" class="chkAll" /><br />
<input type="checkbox" name="chk2" value="val2" class="chkAll" /><br />
<input type="checkbox" name="chk3" value="val3" class="chkAll" /><br />
<input type="checkbox" name="chk4" value="val4" class="chkAll" /><br />
<input type="checkbox" name="chk5" value="val5" class="chkAll" /><br />
<label for="chkSelAll">Select all</label><input type="checkbox" id="chkSelAll" />
<script type="text/javascript">
var chkAllOA = document.getElementsByClassName('chkAll');
document.getElementById('chkSelAll').onclick=function(){
for(i=0; i<chkAllOA.length; i++){
chkAllOA[i].checked = this.checked;
}
}
</script>
</body>

or if you want a button to check/uncheck all the checkboxes, you again really need only 1 button and 1 function:


<body>
<input type="checkbox" name="chk1" value="val1" class="chkAll" /><br />
<input type="checkbox" name="chk2" value="val2" class="chkAll" /><br />
<input type="checkbox" name="chk3" value="val3" class="chkAll" /><br />
<input type="checkbox" name="chk4" value="val4" class="chkAll" /><br />
<input type="checkbox" name="chk5" value="val5" class="chkAll" /><br />
<input type="button" id="btnSelAll" value="Select all" />
<script type="text/javascript">
var chkAllOA = document.getElementsByClassName('chkAll');
var btnSelAll = document.getElementById('btnSelAll');
btnSelAll.checked = false;
btnSelAll.onclick=function(){
this.checked = (this.checked)? false : true;
this.value = (this.checked)? 'Uncheck all' : 'Check all';
for(i=0; i<chkAllOA.length; i++){
chkAllOA[i].checked = this.checked;
}
}
</script>
</body>

Philip M
03-15-2012, 09:39 AM
This may suit you better as it works in all browsers including <IE9:-



<html>
<head>
<title>Toggle Checkboxes</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>

<form name = "myform" action='#'>
<input name = "myname" type = "checkbox" value = "Red"> RED <br>
<input name = "myname" type = "checkbox" value = "Blue"> BLUE <br>
<input name = "myname" type = "checkbox" value = "White"> WHITE <br>
<input name = "myname" type = "checkbox" value = "Green"> GREEN <br>
<input name = "myname" type = "checkbox" value = "Yellow"> YELLOW<br><br>
<input type = "radio" name = "toggle" onclick = "cboxToggle(myname, !this.checked)">UNCHECK ALL<br>
<input type = "radio" name = "toggle" onclick = "cboxToggle(myname, this.checked)">CHECK ALL<br><br>

<input type = "button" value = " CHECK ALL" onclick = "cboxToggle(myname, !this.checked)"><br>
<input type = "button" value = " UNCHECK ALL" onclick = "cboxToggle(myname, this.checked)"><br>

<input type = "button" value = "CHECK/UNCHECK ALL" onclick = "toggleAll(myname)"<br>


</form>

<script type = "text/javascript">

function cboxToggle(group, action) {
for( var i=0, len = group.length; i < len; i++) {
group[i].checked = action;
}
}

var count = 0;
function toggleAll(group) {
if (count == 0) {
for (var i=0, len = group.length; i < len; i++) {
group[i].checked = true;
count = 1;
}
}
else {
for (var i=0, len = group.length; i < len; i++) {
group[i].checked = false;
count = 0;
}

}
}

</script>

</body>

There are not many stewards about - you could count them on the fingers of one hand. There are six of them. - Commentator Live Sports Extra

Philip M
03-15-2012, 10:10 AM
What browsers does the demo I posted not work in?

The browsers I mentioned. Is that not clear? The hard fact is that your code will not work in <IE9. All browsers support document.getElementById(), although I do not understand your point as my code does not use that. IE <9 does not support document.getElementsByClassName(), and even then we find that IE9 requires a !DOCTYPE (not in your code). It is no good talking about "a simple fix". chellert is obviously a newcomer and I doubt if he appreciates that he needs to include additional code before your offering will work reliably in all browsers. Or that he must search for a fix in another thread. Why go out of your way to mislead people when a simpler code would work just fine?

webdev1958
03-15-2012, 10:16 AM
This may suit you better as it works in all browsers including <IE9:-


For browsers that don't support document.getElementsByClassName(), the fix is very easy as I posted in another thread recently :)

You don't need multiple buttons/links or functions to select/deselect all. One link/button and one function is all you need whether document.getElementsByClassName() is supported by the browser or not :thumbsup:

webdev1958
03-15-2012, 10:31 AM
<input name = "myname" type = "checkbox" value = "Red"> RED <br>
<input name = "myname" type = "checkbox" value = "Blue"> BLUE <br>

Giving checkboxes the same name is not normally done and if the form data is going to a php script, the way they are named is not correct.

You normally give the same name to a group of radio buttons.

webdev1958
03-15-2012, 10:42 AM
.....we find that IE9 requires a !DOCTYPE ......

I think you'll find that every web developer worth their salt will tell you that every html page should have a valid doctype as you found out the hard way recently ;). Why you don't have one is beyond me :confused:. At the very least, IE goes into quirksmode when a doctype is missing and that can cause problems.

When posting just a code snippet I obviously don't include a doctype, but when I post the entire html, you will notice that I always include a doctype :)

webdev1958
03-15-2012, 10:48 AM
..... Or that he must search for a fix in another thread.

I don't know if the op even cares if document.getElementsByClassName() is supported by IE8- or not so why spend time retyping something (s)he might not even care about?

Assuming the op is even interested, if the op hasn't the time to spend a minute or so searching my posts then why should I be expected to spend time retyping a post ;).. For me it's a question of priorities and time available.

Philip M
03-15-2012, 10:50 AM
Giving checkboxes the same name is not normally done and if the form data is going to a php script, the way they are named is not correct.

You normally give the same name to a group of radio buttons.



The standard PHP method to indicate that there are multiple controls with the same name requires the use of square brackets in the name of the control.

<input type="checkbox" name="foo[]" value="1"> 1
<input type="checkbox" name="foo[]" value="2"> 2
<input type="checkbox" name="foo[]" value="3"> 3

Here is another ultra-simple toggle script for checkboxes:-


<html>
<head>
<script type = "text/javascript">
var checked = false;
function checkedAll () {
checked == true? checked = false:checked = true;
var els = document.getElementById("mydiv").getElementsByTagName("input");
var len = els.length;
for (var i = 0; i < len; i++) {
els[i].checked = checked;
}
}
</script>
</head>

<body>
<form id="myform">
<div id = "mydiv">
<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
</div>
<br>
Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
</form>
</body>
</html>

webdev1958
03-15-2012, 10:56 AM
The standard PHP method to indicate that there are multiple controls with the same name requires the use of square brackets in the name of the control.


That's correct. The point I was making is that you don't normally give checkboxes the same name as you did, whether they are being sent to a php script or not. And they don't need the same name for the op's requirement in this case either.

You normally give the same name to a group of radio buttons.

webdev1958
03-15-2012, 11:18 AM
Here is another ultra-simple toggle script for checkboxes:-


<html>
<head>
<script type = "text/javascript">
var checked = false;
function checkedAll () {
checked == true? checked = false:checked = true;
for (var i = 0; i < document.getElementById("myform").elements.length; i++) {
document.getElementById("myform").elements[i].checked = checked;
}
}
</script>
</head>
<body>
<form id="myform">
<div id = "mydiv">
<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
<br>
Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
</div>
</form>
</body>
</html>You could also use getElementsByTagName() if there are other form elements.
This is a very poor way of doing it imo because every control in the <form> will then be toggled between checked/unchecked whether it is suppoosed to be or not and whether it is meaningful or not....eg..why toggle a textbox if one was present since a checked property on a textbox is meangless?

So if you have say textboxes or other radio buttons in the form, they also will be toggled which is obviuosly not what you would want.

So a better and more efficient way is to just toggle the required checkboxes and nothing else :)

Philip M
03-15-2012, 11:24 AM
Yes, you are right. I revised the script to take account of this - but I did point out that you could also use getElementsByTagName() if there are other form elements. I accept that in reality there will be other form elements.

webdev1958
03-15-2012, 11:33 AM
Yes, you are right. I revised the script to take account of this - but I did point out that you could also use getElementsByTagName() if there are other form elements. I accept that in reality there will be other form elements.

The red bit depends on the markup.

In a typical case where you are outputing rows of data from a database and adding a checkbox at the end of each row for selecting/deselecting the row to do whatever, the checkboxes will much more often than not be in separate containers and so getElementsByTagName() would not be appropriate.

In terms of maintenace and flexibilty, the easiest and best way imo is to give the relevant checkboxes a class and collect them by their class name in the javascript based on the demo I posted earlier.

By giving them a class, they can then be placed anywhere on the page and you won't have to change the javascript at all. It also won't matter if there are or are not other elements in the <form>.

Handling browsers that don't support document.getElementsByClassName() is a trivial task nowadays.

Philip M
03-15-2012, 11:52 AM
Handling browsers that don't support document.getElementsByClassName() is a trivial task nowadays.

Yes, it is - but you ought to point out to inexperienced users that <IE9 does not support document.getElementsByClassName(), and additional code is required to overcome that. If you don't want to include it and see it as repetitious, then at least give a link to where that code can be found.

webdev1958
03-15-2012, 11:57 AM
Yes, it is - but you ought to point out to inexperienced users that <IE9 does not support document.getElementsByClassName(), and additional code is required to overcome that. If you don't want to include it and see it as repetitious, then at least give a link to where that code can be found.

But you didn't point out to "inexperienced users" the pitfalls and inefficiencies in your code. I did that for you :).

So, why don't you lead by example and in the future point out the pitfalls in your code (at the very least like what can happen if you don't have a valid doctype) and then I will do the same. But like I said, it's a question of time available and priorities for me :thumbsup:

low tech
03-15-2012, 12:01 PM
Hey guys

Just out of curiosity

Couldn't you just collect all the form elements and then just focus on the checkbox by checking its type?

I'm still learning so I'm not sure if that would be a practical solution to anything.

LT


Demo for the purpose of my question only:

<html>
<head>
<script type = "text/javascript">
var checked = false;
function checkedAll () {
checked == true? checked = false:checked = true;
var els = document.getElementById("myform").elements;
var len = els.length;
for (var i = 0; i < len; i++) {
if (els[i].type === "checkbox"){
els[i].checked = checked;
}
}
}
</script>

</head>

<body>
<form id="myform">
<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
<input type="radio" name="rad1">
<input type="radio" name="rad1">
<br>
Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
</form>
</body>
</html>

webdev1958
03-15-2012, 12:06 PM
Hey guys

Just out of curiosity

Couldn't you just collect all the form elements and then just focus on the checkbox by checking its type?



Now we're just starting to go round in circles :eek:

What if you had:


<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
<input type="radio" name="rad1">
<input type="radio" name="rad1">
<input type="checkbox" name="chk4">With your js, chk4 would also be toggled but you might not want it to be :thumbsup:

This was covered earlier in the thread.

Imo, using a class as I showed earlier, is the easiest way and gives you the most flexibility for the reasons I posted earlier.

low tech
03-15-2012, 12:14 PM
Hey webdev1958

forgive the relative beginner hahaha

I just saw this in the first post


I am trying to find a way to use the check all javascript code to select all my checkboxes

and I thought could I check its type ---- no biggie:-)

sorry if I took things in circles.

I've read your post and see what you are saying about the flexibility of using a class

LT

chellert
03-20-2012, 07:37 PM
The code by webdev1958 works well in Firefox, Safari and Chrome but I can't seem to pass the checkboxes to the javascript to make sure the correct checkboxes are selected. If I use the code as is - it will check all boxes under different sections.

The code Philip M provided works well with only IE and I am able to pass the proper checkboxes to be selected but it won't work in Firefox, Safari or Chrome.

I have used both codes determined by which browser is bing used but again I still have the problem with webdev1958's code by determining which checkboxes need to be checked.

Am I missing something? If I use an ID in the class name but when I click on the button it will select all checkboxes under the button and not above it.

Using Philip M's code, I am unable to pass the information from the checkboxes to validate they are checked. it only validates the last checkbox since I don't seem to have a proper array??

Philip M
03-20-2012, 10:23 PM
The code Philip M provided works well with only IE and I am able to pass the proper checkboxes to be selected but it won't work in Firefox, Safari or Chrome.


The code I posted at http://www.codingforums.com/showthread.php?p=1204319 post # 9 (as revised) works in all browsers.

Old Pedant
03-20-2012, 11:07 PM
... you don't normally give checkboxes the same...


Ummm...actually, I do it all the time. And since I don't use PHP I don't need to use the idiotic square brackets that PHP foists on people for some braindead reason.

ASP, JSP, ASP.NET all work fine with multiply named fields, without the need for the square brackets.

As to when multiply named checkboxes are useful: I had an example just yesterday.

I needed a set of checkboxes to indicate which types of payment were accepted by various companies. So...


<label><input type="checkbox" name="paymentType" value="Visa"/><img src="visa.jpg" alt="Visa" /></label>
<label><input type="checkbox" name="paymentType" value="MasterCard"/><img src="mc.jpg" alt="MasterCard" /></label>
<label><input type="checkbox" name="paymentType" value="Discover"/><img src="discover.jpg" alt="Discover" /></label>

Presto. And much simpler to process the array of values I get on the server instead of having to look for individual checkboxes by name.

I can think of lots of other uses for multiply-named checkboxes. Or even multiply-name <input type="text"/> for that matter.

Old Pedant
03-20-2012, 11:10 PM
p.s.: I do agree though that using a class is great if you do not use the same name for the checkboxes.

But another way:


<div id="cbGroup1">
<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
</div>
<input type="radio" name="rad1">
<input type="radio" name="rad1">
<input type="checkbox" name="chk4">
...

var cbs = document.getElementById("cbGrooup1").getElementsByTagName("input");
for ( c = 0; c < cbs.length; ++c )
{
var cb = cbs[c];
...
}

webdev1958
03-20-2012, 11:59 PM
The code by webdev1958 works well in Firefox, Safari and Chrome but I can't seem to pass the checkboxes to the javascript to make sure the correct checkboxes are selected. If I use the code as is - it will check all boxes under different sections.


It works in IE as well :)

But can you elaborate on what you mean by the red bit please. I'm not sure what you mean.

webdev1958
03-21-2012, 12:04 AM
..... And since I don't use PHP I don't need to use the idiotic square brackets that PHP foists on people for some braindead reason.....


The square brackets requirement for sending an array of data to PHP has been there for as long as I can remember and is a trivial issue for me.

Whether php should have that requirement or not is another issue and essentially moot though imo.

To type [] takes next to no time at all and since php is still one of the most popular, if not the most popular, server side scripting languages it seems very few people see the [] thingy as an issue :thumbsup:

Old Pedant
03-21-2012, 12:30 AM
Yeah, yeah, I know. Just one of my soapbox issues. PHP did so many other things right and then messed up on the square brackets. You know, PHP could still fix things, even at this late date, by adding a function or two. Say getCollection( $_POST, "paymentType" ); or some such. But I know it won't happen.

Philip M
03-21-2012, 10:28 AM
p.s.: I do agree though that using a class is great if you do not use the same name for the checkboxes.

But another way:


<div id="cbGroup1">
<input type="checkbox" name="chk1">
<input type="checkbox" name="chk2">
<input type="checkbox" name="chk3">
</div>
<input type="radio" name="rad1">
<input type="radio" name="rad1">
<input type="checkbox" name="chk4">
...

var cbs = document.getElementById("cbGrooup1").getElementsByTagName("input");
for ( c = 0; c < cbs.length; ++c )
{
var cb = cbs[c];
...
}


Quite like the code posted in post #9! :D

Old Pedant
03-21-2012, 08:55 PM
I hadn't seen your code when I posted. Shame on me. Yes, we are in violent agreement, yet again.

chellert
03-27-2012, 01:13 AM
It works in IE as well :)

But can you elaborate on what you mean by the red bit please. I'm not sure what you mean.
Sorry for the time between posts - busy...

What do I mean... OK, so I get the checkboxes to check all - fine and dandy.

Now I need to either deactivate or activate the users once they are all checked. but I can't seem to pass the checkbox array to my script to deactivate/activate the users in the DB.

chellert
03-27-2012, 01:31 AM
OK, the code works in all browsers but now I have another problem - for some reason the bottom un/check all doesn't do the checkboxes above it but the property above it. I have this all in a while loop and it will only recognize the first set of checkboxes unless I am tired and am seeing something wrong....


<a class='comp' onclick='singleHideandShow({$row['pr_id']})' style='cursor:pointer'>{$row['propname']}</a><p>
<form id='myform' method='post' action='script/deac_exec.php'>
<div id='mydiv'>
$propqry = mysql_query("SELECT * FROM users WHERE propid={$row['pr_id']}");

<div id={$row['pr_id']} style='display:none;'>
while($propf = mysql_fetch_assoc($propqry)) {
if($propf['uactive'] == "yes") {
$pactive = "active";
} else {
$pactive = "deactivated";
}
$i++;

input type='checkbox' name='myname".$i."' value='{$propf['us_id']}'>&nbsp;&nbsp;<a href='profile.php?paction=edit&pid={$propf['us_id']}' /><img src='../images/secure/edit.png' name='Edit' border='0' /></a>&nbsp;<a href='profile.php?paction=delete&pid={$propf['us_id']}' /><img src='../images/secure/delete.png' name='Delete' border='0' /></a><label class='cuser'>&nbsp;&nbsp;{$propf['fname']} {$propf['lname']}, &nbsp;&nbsp; ({$propf['uname']}), &nbsp;&nbsp; {$pactive}</label>
}
Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
</div>
</div>
</form>

Old Pedant
03-27-2012, 02:00 AM
Can you show the page live?

Without seeing the JS code and how you ended up implementing checkedAll( ) how can we guess where the problem is?

Mishu
03-27-2012, 04:40 AM
for some reason the bottom un/check all doesn't do the checkboxes above it but the property above it.

To check/uncheck all checkboxes is fairly simple.


<form action="deleteUsers.php" method="post">
<div>
<input type="checkbox" name="delUsers[]" value='id1'/>User 1<br />
<input type="checkbox" name="delUsers[]" value='id2' />User 2<br />
<input type="checkbox" name="delUsers[]" value='id3' />User 3<br />
<input type="checkbox" onclick="selectAll(this)" />(un)check all<br />
<input type="submit" value="Delete users" />
</div>
</form>
<script>
function selectAll(elem){
var chkboxes = document.getElementsByName('delUsers[]');
for(i=0; i<chkboxes.length; i++){
chkboxes[i].checked = elem.checked;
}
}
</script>and then in deleteUsers.php you can build your query to process the passed userID's similar to this


<?php

if(isset($_POST['delUsers'])){
$str = implode(',',$_POST['delUsers']);
$query = 'delete from users where userID in('.$str.')';

echo $query;

//if all users were selected $query is
//delete from users where userID in(id1,id2,id3)
}

?>if the user id's are strings then you will need to wrap them in quotes in $query.

chellert
03-29-2012, 10:58 PM
Can you show the page live?

Without seeing the JS code and how you ended up implementing checkedAll( ) how can we guess where the problem is?
unfortunately I can't provide you with the live site as it's a client's secure area and their site has private information. But I have moved the code into another website so you can view

http://www.zoomized.com/profile.php?paction=edit&find=none

xelawho
03-29-2012, 11:09 PM
you have 2 divs with the same id "mydiv"

and within them are divs with id's starting with numbers (65 and 66)

both of those things are illegal html, and will only cause problems.

I don't know if they're causing *this* problem, but they can't be good...

chellert
04-02-2012, 08:12 PM
I put an unique identifier with the mydiv and nothing works.

xelawho
04-02-2012, 08:28 PM
I am guessing you are testing on another page because the link you provided still has those problems

Dormilich
04-03-2012, 04:23 PM
PHP did so many other things right and then messed up on the square brackets. You know, PHP could still fix things, even at this late date, by adding a function or two. Say getCollection( $_POST, "paymentType" ); or some such. But I know it won't happen.

looks like PHP’s filter functions …

from the PHP point-of-view the square brackets are straightforward, as that is a common method to build PHP arrays.

chellert
04-03-2012, 05:20 PM
Yes I am stilling the problem, I can't figure out why my select all won't work on the second property group. It isn't working in my actual site or this site that was provided.

xelawho
04-03-2012, 06:22 PM
I don't know how I can tell you any differently. You have two divs with the same ID.

Open up the property management div. Then click on your check all checkbox in testing window. See how all the property management boxes get checked?

That's your problem. Rename your divs and it will work.

chellert
04-03-2012, 11:52 PM
i did rename my divs and it doesn't work. because then I would have to pass the div name to the javascript or have separate javascript code for each div.

xelawho
04-04-2012, 02:46 AM
so pass the div id to the function. why not?

chellert
04-04-2012, 11:13 PM
i don't know how to pass it in the javascript code to the javascript function that will do the select on unselect. - not too familiar with javascript:

What do I put in the onclick="checkedAll();" function

and what do I use in the function to capture it

function checkedAll () {
checked == true? checked = false:checked = true;
var els = document.getElementById("mydiv").getElementsByTagName("input");
var len = els.length;
for (var i = 0; i < len; i++) {
els[i].checked = checked;
}
}

xelawho
04-04-2012, 11:35 PM
try this:



<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="mydiv1">
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/><br>
check all<input type="checkbox" onclick="checkedAll(this.checked,'mydiv1')"/>
</div>

<div id="mydiv2">
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/><br>
check all<input type="checkbox" onclick="checkedAll(this.checked,'mydiv2')"/>
</div>


<script type="text/javascript">
function checkedAll (boxchecked,thediv) {
var els = document.getElementById(thediv).getElementsByTagName("input");
var len = els.length;
for (var i = 0; i < len; i++) {
els[i].checked = boxchecked;
}
}

</script>
</body>
</html>

chellert
04-09-2012, 09:32 PM
I get the following error "Message: 'document.getElementById(...)' is null or not an object"

<script type = "text/javascript">
var checked = false;
function checkedAll (boxchecked, thediv) {
checked == true? checked = false:checked = true;
var els = document.getElementById(thediv).getElementsByTagName("input");
var len = els.length;
for (var i = 0; i < len; i++) {
els[i].checked = checked;
}
}
</script>

Dormilich
04-10-2012, 06:55 AM
either that given ID doesn’t exist or you try to access the element before it is created.

Philip M
04-10-2012, 08:44 AM
xelawho's script works fine for me! :)

Spinner
04-10-2012, 02:39 PM
and then in deleteUsers.php you can build your query to process the passed userID's similar to this


<?php

if(isset($_POST['delUsers'])){
$str = implode(',',$_POST['delUsers']);
$query = 'delete from users where userID in('.$str.')';

echo $query;

//if all users were selected $query is
//delete from users where userID in(id1,id2,id3)
}

?>if the user id's are strings then you will need to wrap them in quotes in $query.

Ouch, man. When providing examples like these, please consider a little bit of input-checking first, this is just begging for abuse and exploitation and can render serious damage to the database in question :)

chellert
04-10-2012, 06:56 PM
It finally worked, I had a problem with the single quotes around the onclick function. fixed it.

thanks everyone for your help on this matter.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum