/    Sign up×
Community /Pin to ProfileBookmark

Check All/Clear All Checkboxes

The following PHP code create a simple 2×2 table along with a check box for each row.

Using JavaScript, how do I create a script, that ‘Checks All’ check boxes upon the top (title) check box being clicked, then if the top (title) check box is clicked again, ‘Clears All’ check boxes?

[code=php]<?php
$con = mysql_connect(“localhost”,”peter”,”abc123″);
if (!$con)
{
die(mysql_error());
}

mysql_select_db(“db”, $con);

$result = mysql_query(“SELECT * FROM person”);

echo ‘<table border=”1″>
<tr>
<th><input type=”checkbox” name=”checkbox” value=”checkbox”></th>
<th>First Name</th>
<th>Last Name</th>
</tr>’;

while($row = mysql_fetch_array($result))
{
echo ‘<tr>’;
echo ‘<td><input type=”checkbox” name=”checkbox” value=”checkbox”></td>’;
echo ‘<td>’.$row[‘FirstName’].'</td>’;
echo ‘<td>’.$row[‘LastName’].'</td>’;
echo ‘</tr>’;
}
echo ‘</table>’;

mysql_close($con);
?>[/code]

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 28.2007 — [code=html]
<input type="checkbox" value="1" onclick="var w = document.getElementsByTagName('input'); for(var i = 0; i < w.length; i++){ if(w[i].type='checkbox'){ w[i].checked = this.checked;};};">
[/code]

sorry that it's a bit of a long code, but it works.
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — Can you change it to a JavaScript function?
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — it's not really any different ?

PHP tags used for javascript because it's easier to read.
[code=php]function checkUncheck(){
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type='checkbox'){
w[i].checked = arguments[0].checked;
};
};
};[/code]
call it like so:
[code=html]<input type="checkbox" value="1" onclick="checkUncheck(this);">[/code]
or if you want you can try this variation, which is easier to use with text based links and such:
[code=php]function checkUncheck(){
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type='checkbox'){
w[i].checked = arguments[0];
};
};
};[/code]
[code=html]<input type="checkbox" value="1" onclick="checkUncheck(this.checked);">

OR

<a href="#" onclick="checkUncheck(true);">check all boxes.</a>
<a href="#" onclick="checkUncheck(false);">uncheck all boxes.</a>[/code]
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — OK Cheers.

Upon clicking the 2nd or above check box, how do I clear the top (title) check box?
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — [code=php]function toptoggle(){
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type='checkbox'){
if(w[i].checked != arguments[0].checked){
w[i].checked = arguments[0].checked;
};
return true;
};
};
};[/code]
you then need to add the onclick to all of your checkboxes:
[code=html]<input type="checkbox" onclick="toptoggle(this);" ...[/code]
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — Can you change the topToggle() code, as the 2nd or above check boxes shouldn't recheck the top (title) check box.
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — if you don't want them to affect it then just don't add the onclick to them. if on the other hand you don't want them affected by the top checkbox it will take a small edit to make it ignore them
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — The top (title) check box should check/clear the 2nd and above check boxes, but the 2nd and above check boxes should only clear the top (title) check box.

Can the code be changed inorder to do this?
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — [code=php]function toptoggle(){
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type='checkbox'){
w[i].checked = false;
return true;
};
};
};[/code]
sorry, didn't get what you meant at first ?
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — If the 2nd and above check boxes are all checked, is it possible to check the top (title) check box?
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — that's more code....

[code=php]function toptoggle(){
var firstbox = false;
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type=='checkbox'){
if(!firstbox){
firstbox = w[i];
}else{
if(!w[i].checked){
firstbox.checked = false;
return true;
};
};
};
};
if(firstbox)
firsbox.checked = true;
return true;
};[/code]
not tested this one though

edited twice, I should read more carefully.
Copy linkTweet thisAlerts:
@DysanauthorDec 28.2007 — It doesn't work, the code has an error. Why is this?
Copy linkTweet thisAlerts:
@scragarDec 28.2007 — works fine for me after my 2 little errors got fixed, why don't you try copying it again.
Copy linkTweet thisAlerts:
@DysanauthorJan 01.2008 — If the 2nd and above check boxes are all manually checked, is it possible to check the top (title) check box?
Copy linkTweet thisAlerts:
@McaGuyJan 08.2008 — [code=php]
<input type="checkbox" value="1" onclick="var w = document.getElementsByTagName('input'); for(var i = 0; i < w.length; i++){ if(w[i].type='checkbox'){ w[i].checked = this.checked;};};">
[/code]

sorry that it's a bit of a long code, but it works.[/QUOTE]

it still not working
Copy linkTweet thisAlerts:
@scragarJan 08.2008 — that code worked in my tests, so either:

you copied it wrong

OR

you need to show me what you have so I can locate the problem.

I'm guessing after so long you would have copied my edits, so maybe you should post a link or some generated HTML(I'm not intrested in the PHP)
×

Success!

Help @Dysan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...