View Full Version : Checkbox script
Wobbler
09-08-2003, 05:10 PM
I've got this code that selects all checkbox in a document if tou hit a button. Kind of like hotmail select all mail system. And i use the function below.
It works fine, BUT when it's only one checkbox it doesn't get selected, only when there are two and more checkboxes with the same name they get selected. Help, anybody?? Thnx i before...
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Avmarkera alla"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Markera alla"; }
}
// End -->
</script>
cheesebag
09-08-2003, 05:17 PM
Please post a complete example (a webpage, i.e.) of what isn't working.
Wobbler
09-08-2003, 05:28 PM
Why? All you need to know is written down.
The code is working, but not when tehe is only *one* checkbox on the page, else the script works just fins. btw that is all the code... Called by a button.
<input type=button value="Markera alla" onClick="this.value=check(this.form.delID)">
cheesebag
09-08-2003, 06:02 PM
All you need to know is written down....If you knew that, how come you can't fix the script? :rolleyes: Why argue with people trying to help you?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ugh</title>
<script type="text/javascript" language="javascript">
function check_uncheck_all(oButton, field) {
var bWhich = (oButton.value == 'Markera alla');
if (typeof field.length != 'undefined')
for (var i = 0; i < field.length; ++i)
field[i].checked = bWhich;
else field.checked = bWhich;
oButton.value = (bWhich) ? 'Avmarkera alla' : 'Markera alla';
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="delID"><br /><br />
<input type="button" style="width:100px;" value="Markera alla" onclick="check_uncheck_all(this,delID)">
</form>
</body>
</html>
sage45
09-08-2003, 06:37 PM
Well actually, all the information that you included originally would not have been enough by itself... We still needed to see how you were referencing the script via your page... But that's besides the point... I assumed that you were using it this way:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
if (field.length == "0") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Avmarkera alla";
}
else {
field.checked = true;
}
checkedflag = "true";
return "Avmarkera alla";
}
else {
if (field.length == "0") {
for (i = 0; i < field.length; i++) {
field[i].checked = false;
}
checkflag = "false";
return "Markera alla";
}
else {
field.checked = false;
}
checkedflag = "false";
return "Markera alla";
}
}
// End -->
</script>
...
...
...
<form name=somenew>
<input type=checkbox name=delID>
<input type=button value="Markera alla" onClick="this.value=check(this.form.delID)">
</form>
I got it to work to the point of checking false on the checkbox... Essentially your issue comes from this:
for (i = 0; i < field.length; i++)
If you have one checkbox, it's i value would be represented as "0"... Because of this, you need to ensure that the field.length is larger than 0:
if (field.length == "0")
-or-
if (field.length > "0")
If the field.length is not larger than 0 then the script assumes that their is only one and checks it...
As I said, I just can't get it to uncheck it... Grrr... Stoopid VBA is foofing my JScript all around... :(
I'll keep hammering on it, till I find out why it doesn't complete or until someone else comes up with a more elegant way of completing this... :D
P.S. --> Nice one Cheesebag... :p I promote you to Cheesegrater... :D
P.P.S --> Cheesebags method is the preferred and more elegant/efficient of the methods posted... :D
HTH,
-sage-
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.