Modifying a checkbox script

C

calzephyr

Hi group,

I'm trying to modify a checkbox script. Currently it will select all
or deselect all. What we would like it to do now is reverse the
selection, so that the boxes that were checked become unchecked, and
vice versa. It's been a long time since I had to modify JavaScript and
I'm a little stuck. Here is the script below - the function I'm trying
to create is called inverseAll.

Thanks in advance,
Christine

<script language="JavaScript" type="text/javascript">
<!--

function setCheckAll(checkedState) {
var formElements = document.forms[0].elements;
var elementCount = formElements.length;
for(var i=0; i<elementCount; i++) {
var control = formElements;
if (control != null) {
if (control.type != null) {
if (control.type == "checkbox") {
control.checked = checkedState;
}
}
}
}
}
function checkAll() {
setCheckAll(true);
}
function uncheckAll() {
setCheckAll(false);
}
function inverseAll() {
//if setCheck equals true, uncheck
//if setCheck equals false, check
control.checked = !control.checked;
}
// -->
</script>
 
E

Evertjan.

wrote on 04 aug 2007 in comp.lang.javascript:
Hi group,

I'm trying to modify a checkbox script. Currently it will select all
or deselect all. What we would like it to do now is reverse the
selection, so that the boxes that were checked become unchecked, and
vice versa. It's been a long time since I had to modify JavaScript and
I'm a little stuck. Here is the script below - the function I'm trying
to create is called inverseAll.

Thanks in advance,
Christine

<script language="JavaScript" type="text/javascript">

Do not use language="JavaScript"

Do not use <!--
function setCheckAll(checkedState) {
var formElements = document.forms[0].elements;
var elementCount = formElements.length;
for(var i=0; i<elementCount; i++) {
var control = formElements;
if (control != null) {
if (control.type != null) {


why those tests?
formElements.length will have errored if so, meseems.
if (control.type == "checkbox") {
control.checked = checkedState;
}
}
}
}
}
function checkAll() {
setCheckAll(true);
}
function uncheckAll() {
setCheckAll(false);
}
function inverseAll() {
//if setCheck equals true, uncheck
//if setCheck equals false, check
control.checked = !control.checked;
}
// -->

do not use // -->
</script>

function inverseAll() {
var formElements = document.forms[0].elements;
var elementCount = formElements.length;
for(var i=0; i<elementCount; i++) {
var control = formElements;
if (control.type == "checkbox") {
control.checked = !control.checked;
};
};
};
 
E

Evertjan.

Evertjan. wrote on 04 aug 2007 in comp.lang.javascript:
function inverseAll() {
var formElements = document.forms[0].elements;
var elementCount = formElements.length;
for(var i=0; i<elementCount; i++) {
var control = formElements;
if (control.type == "checkbox") {
control.checked = !control.checked;
};
};
};


Using "this" makes the function usable in more forms on the page:

<script type='text/javascript'>
function inverseAll(f) {
var formElements = f.form.elements;
var elementCount = formElements.length;
for(var i=0; i<elementCount; i++) {
var control = formElements;
if (control.type == "checkbox") {
control.checked = !control.checked;
};
};
};
</script>


<form>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type=button onclick='inverseAll(this)'
value='Inverse all'>
</form>
<hr>
<form>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type='checkbox'><br>
<input type=button onclick='inverseAll(this)'
value='Inverse all'>
</form>
 
C

calzephyr

Hi Evertjan, thanks for your help! I'm just going to try some things
here...
Do not use language="JavaScript"

Things must have really changed in the past few years! Why is it no
longer necessary to specify the language? I'll have to pick up a new
O'Reilly reference...maybe it's just as well that I left my old one
with a co-worker when I changed jobs.

Best,
Christine
 
E

Evertjan.

wrote on 05 aug 2007 in comp.lang.javascript:
Hi Evertjan, thanks for your help! I'm just going to try some things
here...


Things must have really changed in the past few years! Why is it no
longer necessary to specify the language? I'll have to pick up a new
O'Reilly reference...maybe it's just as well that I left my old one
with a co-worker when I changed jobs.

For the moment, this is the way to go:

<script type='text/javascript'>
</script>

Read the NG FAQ, it is perhaps not better, but certainly more up to date,
Christine, than any printed book after the first few months.

<http://jibbering.com/faq/index.html>
 
C

calzephyr

Thanks again Evertjan, I've got it bookmarked. Oh, that remimds me, I
have some hideously old JavaScript book from the days of 4th gen
browsers. It's kind of a shame that all it's good for is a doorstop
now :-D

Christine
 
C

calzephyr

Hi Evan,

Thanks for taking a shot at this :) I was wondering if there was some
way to take the original and add something else in. There does seem to
be a problem with it though...I get an error in IE 6 saying that
checkedState is undefined on a line that matches up to the inverseAll
function. I tried renaming it like so - function inverseAll(foo) and
= !foo - so it would have a name. It kind of worked - the unselected
checkboxes became selected, but the checked one did stayed checked. If
I click again on my inverse link call to action, all the boxes remain
checked. The way Evertjan modified it works perfectly though, so you
were on the right track. It seems those two control and null
statements were the problem.

Thanks again,
Christine
 
C

calzephyr

Hi again,

Thanks so much, Evertjan, that works like a charm! I tested it in
Firefox 1.5 and 2.0, IE 6 and 7, all on a PC. When I get back to work
I'll be able to try it out on more.

Christine
 
E

Evertjan.

wrote on 05 aug 2007 in comp.lang.javascript:
Thanks so much, Evertjan, that works like a charm! I tested it in
Firefox 1.5 and 2.0, IE 6 and 7, all on a PC. When I get back to work
I'll be able to try it out on more.

Good to hear that "it" worked,
however this being not email but usenet,
with many readers and an indefined time archive,
it would be considerate to always quote what you are replying on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top