NEWBIE uncheck all boxes in form?

M

marcokrechting

Hi All,

I have a htm form with a lot of check boxes and I'm trying to create a
button that will uncheck all boxes at once. The code I have is not
working, any suggestions?


function uncheckTaken()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
if(document.zoekm.first.checked==true)
{
zmv[0]=document.zoekm.first.value;document.zoekm.first.checked=false;}
}
var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken is niet mogelijk met de
versie van uw browser (3.0)");
}

<input type="button" value="Empty All" onClick="uncheckTaken()">
 
L

Lee

(e-mail address removed) said:
Hi All,

I have a htm form with a lot of check boxes and I'm trying to create a
button that will uncheck all boxes at once. The code I have is not
working, any suggestions?


function uncheckTaken()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
if(document.zoekm.first.checked==true)
{
zmv[0]=document.zoekm.first.value;document.zoekm.first.checked=false;}
}
var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken is niet mogelijk met de
versie van uw browser (3.0)");
}

<input type="button" value="Empty All" onClick="uncheckTaken()">



"not working" doesn't tell us very much.
It looks like your code is doing a lot more than just trying
to uncheck all checkboxes.

A couple of things stand out, though. There's no need to use
eval() to test the value of:
document.zoekm.elements[(i*2)+1].checked
and there's certainly no reason to compare that value to "true",
but I without knowing what you're really trying to do, I can't
make any specific suggestions.
 
R

RobG

Hi All,

I have a htm form with a lot of check boxes and I'm trying to create a
button that will uncheck all boxes at once. The code I have is not
working, any suggestions?


function uncheckTaken()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){

Is almost never a good reason for browser sniffing, use feature
detection. Your script requires support for minimal features, but if
you really want to do it, test for support for the forms collection and
leave it at that:

if ( document.forms ) {

If the function is called from a control inside the form, it can pass
'this' and you become independent of even the forms collection:


HTML:
<form name="zoekm" ... >
<input type="button" ... onClick="uncheckTaken(this)">
...

Script:
function uncheckTaken( f ) { // f is a reference to the control
f = f.form; // f is now a reference to the form 'zoekm'

zmv=new Array();

zmv = [];

Fewer keystrokes with exactly the same result.
for(i=0; i<(aantalZm);i++)

You create i as a global variable, quite dangerous for counters. It is
not clear where 'anntalZm' comes from or what its value is.

for( var i=0; i<(aantalZm); i++ )
{
if(document.zoekm.first.checked==true)


It's more efficient to store references in local variables, it saves
lookups which slow things down, e.g.:

var f = document.zoekm;
var el = f.first;
if( el.checked==true )
{
zmv[0]=document.zoekm.first.value;
document.zoekm.first.checked=false;
}


zmv[0] = el.value;
el.checked=false;}

and so on...
}
var j=0;
for(i=0; i<aantalZm; i++)

Here you initialise 'j', but then use i as the counter. Since this is
inside a loop that is already using i, I guess you really meant to use j
- but I really have no idea what you are doing:

for( var j=0; j<aantalZm; j++ )

Again, no idea where 'aantalZm' comes from or what its value might be.
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true

A good rule is never, ever use eval. There is almost never a need for
it, and certainly not here. As pointed out by Lee, you don't need to
explicitly compare the checked status to true:

if( f.elements[(i*2)+1].checked ...
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}



Continuing the code-reduction strategy:

if( f.elements[(i*2)+1].checked && el.value != zmv[0])
{
zmv[++j] = el.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else
window.alert("Het afvinken van taken is niet mogelijk met de
versie van uw browser (3.0)");
}

Maybe the above line is not broken in your editor, it highlights not
using tabs when posting here. It is also good to always use braces for
if blocks (though sometimes simple tests with a brief statement may be
better on a single line):

} else
window.alert("Het afvinken van taken is niet mogelijk met "
+ " de versie van uw browser (3.0)");
}
}

There appear to be some missing closing braces...
<input type="button" value="Empty All" onClick="uncheckTaken()">
Have you considered a reset button? No JavaScript required.

A very simple script to uncheck all checkboxes would be:

function clearAllCheckboxes ( f ) {
var el, els = f.elements;
var i = els.length;
while ( i-- ) {
el = els;
if ( 'input' == el.nodeName.toLowerCase()
&& 'checkbox' == el.type ) {
el.checked = false;
}
}
}

....

<form action="">
<input type="checkbox" ...>
<input type="checkbox" ...>
<input type="checkbox" ...>
...
<input type="button" value="Clear checkboxes" onclick="
clearAllCheckboxes(this);
">
</form>



Here is your script cleaned up:

function uncheckTaken() {
if ( document.forms ){
var el, f = document.zoekm;
zmv = [];

for( var i=0; i<(aantalZm); i++ ) {
el = f.first;

if( el.checked ) {
zmv[0] = el.value;
el.checked = false;
}

for( var j=0; j<aantalZm; j++ ) {
if( f.elements[(i*2)+1].checked && el.value != zmv[0]) {
zmv[++j] = el.value;
}

zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";
}
}
} else {
alert( 'Sorry, this script is dependent on support for the'
+ ' forms collection, your browser doesn\'t seem to have it.');
}
}
 
M

marcokrechting

Rob,

Thanks for all the help.
I tried your bit but it didn't work that well.
Maybe I have to be more specific.

<script language="javascript">
var zmdv=new Array(0,17,18,12,28,1,31,2,3,29,20,6,30,27,11)
var zmv = new Array();
zmv=zmdv;

document.write('<input type=radio name=first value=17><input
type=checkbox name=check17><font size=2
face="Arial">MijnTaak</font><br>');
document.write('<input type=radio name=first value=13><input
type=checkbox name=check13><font size=2
face="Arial">MijnTaak2</font><br>');
etc......

for(i=0;i<zmv.length;i++)
{
eval('document.zoekm.check'+zmv+'.checked=true');
}
aantalZm=document.zoekm.elements.length/2;

for(i=0;i<aantalZm;i++)
if
(zmv[0]==document.zoekm.first.value)document.zoekm.first.checked=true;
</script>

function uncheckTaken() {
if ( document.forms ){
var el, f = document.zoekm;
zmv = [];

for( var i=0; i<(aantalZm); i++ ) {
el = f.first;

if( el.checked ) {
zmv[0] = el.value;
el.checked = false;
}

for( var j=0; j<aantalZm; j++ ) {
if( f.elements[(i*2)+1].checked && el.value != zmv[0]) {
zmv[++j] = el.value;
}

zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";
}
}
} else {
alert( 'Sorry, this script is dependent on support for the'
+ ' forms collection, your browser doesn\'t seem to have
it.');
}

}

I have all these radiobuttons and checkboxes on the form.
You can forget about the radiobuttons, they don't mean anything here.
The checkboxes I want to uncheck all at once.

<input type="button" value="Leegmaken" onClick="uncheckTaken()">

The function you suggested is not working.

Regards
Marco
The Netherlands


RobG schreef:
Hi All,

I have a htm form with a lot of check boxes and I'm trying to create a
button that will uncheck all boxes at once. The code I have is not
working, any suggestions?


function uncheckTaken()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){

Is almost never a good reason for browser sniffing, use feature
detection. Your script requires support for minimal features, but if
you really want to do it, test for support for the forms collection and
leave it at that:

if ( document.forms ) {

If the function is called from a control inside the form, it can pass
'this' and you become independent of even the forms collection:


HTML:
<form name="zoekm" ... >
<input type="button" ... onClick="uncheckTaken(this)">
...

Script:
function uncheckTaken( f ) { // f is a reference to the control
f = f.form; // f is now a reference to the form 'zoekm'

zmv=new Array();

zmv = [];

Fewer keystrokes with exactly the same result.
for(i=0; i<(aantalZm);i++)

You create i as a global variable, quite dangerous for counters. It is
not clear where 'anntalZm' comes from or what its value is.

for( var i=0; i<(aantalZm); i++ )
{
if(document.zoekm.first.checked==true)


It's more efficient to store references in local variables, it saves
lookups which slow things down, e.g.:

var f = document.zoekm;
var el = f.first;
if( el.checked==true )
{
zmv[0]=document.zoekm.first.value;
document.zoekm.first.checked=false;
}


zmv[0] = el.value;
el.checked=false;}

and so on...
}
var j=0;
for(i=0; i<aantalZm; i++)

Here you initialise 'j', but then use i as the counter. Since this is
inside a loop that is already using i, I guess you really meant to use j
- but I really have no idea what you are doing:

for( var j=0; j<aantalZm; j++ )

Again, no idea where 'aantalZm' comes from or what its value might be.
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true

A good rule is never, ever use eval. There is almost never a need for
it, and certainly not here. As pointed out by Lee, you don't need to
explicitly compare the checked status to true:

if( f.elements[(i*2)+1].checked ...
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}



Continuing the code-reduction strategy:

if( f.elements[(i*2)+1].checked && el.value != zmv[0])
{
zmv[++j] = el.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else
window.alert("Het afvinken van taken is niet mogelijk met de
versie van uw browser (3.0)");
}

Maybe the above line is not broken in your editor, it highlights not
using tabs when posting here. It is also good to always use braces for
if blocks (though sometimes simple tests with a brief statement may be
better on a single line):

} else
window.alert("Het afvinken van taken is niet mogelijk met "
+ " de versie van uw browser (3.0)");
}
}

There appear to be some missing closing braces...
<input type="button" value="Empty All" onClick="uncheckTaken()">
Have you considered a reset button? No JavaScript required.

A very simple script to uncheck all checkboxes would be:

function clearAllCheckboxes ( f ) {
var el, els = f.elements;
var i = els.length;
while ( i-- ) {
el = els;
if ( 'input' == el.nodeName.toLowerCase()
&& 'checkbox' == el.type ) {
el.checked = false;
}
}
}

...

<form action="">
<input type="checkbox" ...>
<input type="checkbox" ...>
<input type="checkbox" ...>
...
<input type="button" value="Clear checkboxes" onclick="
clearAllCheckboxes(this);
">
</form>



Here is your script cleaned up:

function uncheckTaken() {
if ( document.forms ){
var el, f = document.zoekm;
zmv = [];

for( var i=0; i<(aantalZm); i++ ) {
el = f.first;

if( el.checked ) {
zmv[0] = el.value;
el.checked = false;
}

for( var j=0; j<aantalZm; j++ ) {
if( f.elements[(i*2)+1].checked && el.value != zmv[0]) {
zmv[++j] = el.value;
}

zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";
}
}
} else {
alert( 'Sorry, this script is dependent on support for the'
+ ' forms collection, your browser doesn\'t seem to have it.');
}
}
 
A

ASM

RobG said:
if(document.zoekm.first.checked==true)


It's more efficient to store references in local variables, it saves
lookups which slow things down, e.g.:


Hi,
sorry my english yet so poor ... and probably I mix everything

*lookups* is not in my dictionary :-(

understood : instruction by little bits (variable*s*) (lookup*s*)
is better than in one piece
if it is ->
not unterstood : in what that accelerate something ?
overall if sub-variables used only once.
and yet more if one sub-variable is changed x times
var f = document.zoekm;
var el = f.first;
if( el.checked==true )


if idea to remember is : *to store ref in variable is better*
why not tho have :
var el = f.elements['first'];
if(el.checked)
as f did set ( f = f.form )

or (if I missed sommething)
var el = document.zoekm.elements['first'];
if(el.checked)
as f will probably no more used

I would better understand :
var els = f.elements['first']
// or : var els = document.zoekm.elements['first'];
if(els.checked)

anyway I do not understand in what
if(f.first.checked)
would whatever slow down

using : el = f.first
seems to me more an help for programmer than for processor

A very simple script to uncheck all checkboxes would be:

and in this script variable 'el' doesn't sem really usefull
function clearAllCheckboxes ( f ) {
var el, els = f.elements;
var i = els.length;
while ( i-- ) {
el = els;
if ( 'input' == el.nodeName.toLowerCase()
&& 'checkbox' == el.type ) {
el.checked = false;
}



while(i--)
with(els) {
if('input' == nodeName.toLowerCase() && 'checkbox' == type )
checked = false;
}

is condition on 'input' really necessary ?
(do not think my NC4.5 knows 'nodeName')
(except checkboxes or radios any element can be checked)

what do you think about : if(!(!type) && 'checkbox'==type)
 
A

ASM

Rob,

Thanks for all the help.
I tried your bit but it didn't work that well.
Maybe I have to be more specific.

<script language="javascript">
var zmdv=new Array(0,17,18,12,28,1,31,2,3,29,20,6,30,27,11)
var zmv = new Array();
zmv=zmdv;

document.write('<input type=radio name=first value=17><input
type=checkbox name=check17><font size=2
face="Arial">MijnTaak</font><br>');
document.write('<input type=radio name=first value=13><input
type=checkbox name=check13><font size=2
face="Arial">MijnTaak2</font><br>');
etc......

for(i=0;i<zmv.length;i++)
{
eval('document.zoekm.check'+zmv+'.checked=true');
document.zoekm.elements['check'+zmv].checked=true

}
aantalZm=document.zoekm.elements.length/2;

for(i=0;i<aantalZm;i++)
if
(zmv[0]==document.zoekm.first.value)document.zoekm.first.checked=true;


for(i=0;i<aantalZm;i++)
with(document.zoekm.first){checked = (zmv[0]==value)? true : '';}
</script>

[snip]

I am not very sure about what you want to do ... try this

<script language="javascript">
var zmdv=new Array(0,17,18,12,28,1,31,2,3,29,20,6,30,27,11)
var zmv = new Array();
zmv=zmdv;

document.write('<input type=radio name=first value=17><input
type=checkbox name=check17><font size=2
face="Arial">MijnTaak</font><br>');
document.write('<input type=radio name=first value=13><input
type=checkbox name=check13><font size=2
face="Arial">MijnTaak2</font><br>');
etc......

</script>
.../...
</form>

<script type="text/javascript">

if(document.forms)
var f = document.forms['zoekm'];
var aantalZm = f.length/2;

// do you really want to check all of radios and checkboxes ?

for(i=0;i<zmv.length;i++)
f.elements['check'+zmv].checked = true; // would be false no?

for(i=0;i<aantalZm;i++)
with(f.elements['first']) {
if(zmv[0]==value) checked = true; // would be false no?
}

function uncheckTaken() {
if(document.forms) {
zmv = [];
for(var i=0; i<aantalZm; i++ ) {
var el = f.elements['first'];
if( el.checked) {
zmv[0] = el.value;
el.checked = false;
}
}
for(var j=0; j<aantalZm; j++ ) {
var first = f.elements[j*2];
var check = f.elements[j*2+1];
if( first.checked && zmv[0] && check.value != zmv[0])
zmv[j] = check.value;
}
zmcv=zmv.join(',');
setCookie("zoek",zmcv);
top.location="index.htm";
}
else
alert('Sorry, this script is dependent on support for the ' +
'forms collection, your browser doesn\'t seem to have it.');
}
 
R

RobG

Rob,

Thanks for all the help.
I tried your bit but it didn't work that well.
Maybe I have to be more specific.

<script language="javascript">

The language attribute is depreciated, type is required:

var zmdv=new Array(0,17,18,12,28,1,31,2,3,29,20,6,30,27,11)
var zmv = new Array();
zmv=zmdv;

An empty array called zmv is created so you have two arrays. This is
then replaced with a reference to zmdv - you now have two references
to the array created when zmdv was declared (i.e. there is only 1
array). If you want zmv to be a copy of zmdv, then:

var zmv = [];
for ( i in zmdv ) {
zmv = zmdv;
}

I have no idea why you want to do this, zmdv is not used at all and
zmv is not modified.
document.write('<input type=radio name=first value=17><input
type=checkbox name=check17><font size=2
face="Arial">MijnTaak</font><br>');
document.write('<input type=radio name=first value=13><input
type=checkbox name=check13><font size=2
face="Arial">MijnTaak2</font><br>');
etc......

This could be replaced with a loop:

var txt = [];
for ( i in zmv ) {
txt.push(
'<input type=radio name=first value=',
i,
'><input type=checkbox name=check',
i,
'><span style="font-family: arial, san-serif;',
' font-size: 90%;">MijnTaak<\/span><br>'
);
}
document.write( txt.join('') );

The above implies that it is inside a script element inside the form
'zoekm', so any calls to document.zoekm will not work - the form
hasn't finished being created yet.

for(i=0;i<zmv.length;i++)
{
eval('document.zoekm.check'+zmv+'.checked=true');
}
aantalZm=document.zoekm.elements.length/2;


Does this just check all the checkboxes? If so, it may be that they
aren't created yet and the form zoekm almost certainly hasn't. Check
them all at the end.

Since you create two elements for every member of the zvm array, then
aantalZm is just the length of the zvm array - saves getting the form
elements collection for no good reason.

aantalZm = zvm.length;
for(i=0;i<aantalZm;i++)
if
(zmv[0]==document.zoekm.first.value)document.zoekm.first.checked=true;


This looks like it is trying to check all the radio buttons - but only
one radio button of a group can be checked at a time, so it will check
each one in turn, and as that happens, the previous one will be unchecked.

And one button should always be checked, I'd just check the first one.

document.zoekm.elements[0].checked = true;

But again, you must wait for the elements to finish being created
before you do this, so move it to an onload fuction or after the form
body.


Below is the best I can make of your script.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Radios & Checkboxes</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

<script type="text/javascript">

var zmdv = [ 0, 17, 18, 12, 28, 1, 31, 2, 3, 29, 20, 6, 30, 27, 11 ];
var zmv = [];

// Copy zmdv to zmv
for ( i in zmdv ) {
zmv = zmdv;
}

// f is a reference to a form
// tick is true or false (check or uncheck)
function doCheckboxes( f, tick ){
var el, els = f.elements;
var i = els.length;
while ( i-- ) {
el = els;
if ( 'input' == el.nodeName.toLowerCase()
&& 'checkbox' == el.type ){
el.checked = tick;
}
}
}

</script>
</head><body>

<form name="zoekm" action="">
<div>
<script type="text/javascript">
// Create the form elements
var txt = [];
for ( i in zmv ) {
txt.push(
'<input type=radio name=first value=',
i,
'><input type=checkbox name=check',
i,
'><span style="font-family: arial, san-serif;',
' font-size: 90%;">MijnTaak<\/span><br>'
);
}
document.write( txt.join('') );

</script>
<input type="button" value="Check all checkboxes" onclick="
doCheckboxes( this.form, true );
">
<input type="button" value="Unheck all checkboxes" onclick="
doCheckboxes( this.form, false );
">
</form>

<script type="text/javascript">
// Check all the checkboxes and the first radio
doCheckboxes( document.forms['zoekm'], true );
document.forms['zoekm'].elements['first'][0].checked= true;

</script>
</div>
</body></html>
 
M

marcokrechting

Rob,

Thanks that works fine.
There is only one problem I have to deal with. When I uncheck
all boxes I have to click save to write the settings topm y cookie.
When I do so the checkbox next to the radiobutton that is checked
gets checked again.

function saveGegevens()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
if(document.zoekm.first.checked==true)
{ zmv[0]=document.zoekm.first.value;
eval('document.zoekm.check'+document.zoekm.first.value+'.checked=true')}
}
var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken niet mogelijk met de versie
van uw browser (3.0)");
}

Can this code be changed so it doesn't do that?

Regards
Marco
 
R

RobG

Rob,

Thanks that works fine.
There is only one problem I have to deal with. When I uncheck
all boxes I have to click save to write the settings topm y cookie.
When I do so the checkbox next to the radiobutton that is checked
gets checked again.

Because your code tells it to be checked, see below.
function saveGegevens()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){

I don't think this does anything useful. As noted previously, test for
support for features actually used - trying to guess what may or may not
be supported based on very unreliable browser detection is likely going
to cause far more issues that it solves (if it solves anything at all).

In the above test, some non-IE browser of version 1 (say Firefox) may
well be refused support, even though the features required to support
your script are trivial.

Because page developers insist on using flawed logic such as the test
above, browser vendors make their browsers report whatever appName or
appVersion they feel like. Many allow users set their browser to
masquerade as whatever they want.

Test for document.forms support, though I'd be surprised if any browser
in current use would fail.
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
if(document.zoekm.first.checked==true)
{ zmv[0]=document.zoekm.first.value;
eval('document.zoekm.check'+document.zoekm.first.value+'.checked=true')}
}



I thought we discussed 'eval' before. It is totally unnecessary, don't
use it. Also using global variables for counters is bad practice, the
brackets around aantalZm are unnecessary, you don't need to compare to
'true' explicitly and a local variable to store the value of
document.zoekm will make code simpler.

The line that is checking the checkbox is commented below:

function saveGegevens()
{
if { ! document.forms ) return;
var f = document.zoekm;
var zmv = [];
for( var i=0; i<aantalZm; i++ )
{
if ( f.first.checked )
{
zmv[0] = f.first.value;

// This line here is setting a checkbox to checked
f.check[ f.first.value ].checked = true;
}

}

var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}


Get rid of eval:

var j=0;
for( i=0; i<aantalZm; i++ )
{
if ( f.elements[ ((i*2)+1) ].checked )
&& f.first.value != zmv[0])
{
zmv[++j] = f.first.value;
}
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken niet mogelijk met de versie
van uw browser (3.0)");
}

If you have no further use for the global variable zmcv, it doesn't need
to be created:

setCookie( 'zoek', zmv.join(';') );
top.location = 'index.htm';
}
Can this code be changed so it doesn't do that?

Yes.


function saveGegevens()
{
// Test for support for forms collection
if { ! document.forms ) return;
var f = document.zoekm;
var zmv = [];
for( var i=0; i<aantalZm; i++ )
{
if ( f.first.checked )
{
zmv[0] = f.first.value;

// This line here is setting a checkbox to checked
f.check[ f.first.value ].checked = true;
}

}
var j=0;
for( i=0; i<aantalZm; i++ )
{
if ( f.elements[ ((i*2)+1) ].checked )
&& f.first.value != zmv[0])
{
zmv[++j] = f.first.value;
}
}
setCookie( 'zoek', zmv.join(';') );
top.location = 'index.htm';
}
 
M

marcokrechting

Rob,

I tried to remove a part of the code but that works only once. It will
remove the checked box but when I enter the page again; check all the
boxes and save it won't save the settings. Here's my whole code to
explain:

=================== CODE START ==============================
<HTML>
<HEAD>
<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-control" CONTENT="no-cache">
<TITLE>Smart Download Taken Afvinken</TITLE>
<script language="javascript">
var zmdv=new Array(0,17,18,12,28,1,31,2,3,29,20,6,30,27,11)
var zmv = new Array();
zmv=zmdv;

if ((zmcv=getCookie("zoek"))!='*')
zmv=zmcv.split(';');


function getCookie (cookieNaam) {
var cname = cookieNaam + "=";
var i = 0;
while (i < document.cookie.length) {
var j = i + cname.length;
if (document.cookie.substring(i, j) == cname){
var leng = document.cookie.indexOf (";", j);
if (leng == -1) leng = document.cookie.length;
return unescape(document.cookie.substring(j, leng));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return "*";
}
function delCookie (cookieNaam)
{
document.cookie = cookieNaam + "=; expires= Thu, 01-Jan-70 00:00:01
GMT";
document.cookie = cookieNaam + "=; expires= Thu, 01-Jan-70 00:00:01
GMT; path=/";
return true;
}

function setCookie(cookieNaam, cookieInhoud){
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 10000);
document.cookie = cookieNaam + "= " + escape (cookieInhoud) +";
expires=" + expiry.toGMTString()+ "; path/";};


function resetForm()
{

delCookie("zoek");
//window.location.reload();
top.location="index.htm";
}


function saveGegevens()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
// if(document.zoekm.first.checked==true)
// { zmv[0]=document.zoekm.first.value;
eval('document.zoekm.check'+document.zoekm.first.value+'.checked=true')}
}
var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken niet mogelijk met de versie
van uw browser (3.0)");
}


function doCheckboxes( f, tick ){

var el, els = f.elements;
var i = els.length;
while ( i-- ) {
el = els;
if ( 'input' == el.nodeName.toLowerCase()
&& 'checkbox' == el.type ){
el.checked

= tick;
}
}

alert("Klik 'Opslaan' om de wijzigingen definitief te maken...");
}


</script>
</HEAD>
<BODY bgcolor="#fffacd" text="#000000" link="#000000" vlink="#000000"
alink="#000000">
<center>
<form name="zoekm">
<table name=zoektabel width=80% BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr>
<td colspan="3"><FONT SIZE="2" FACE="Arial">Hieronder vindt u de taken
terug die dagelijk worden uitgevoerd.<br>
Wanneer u alle taken uit een blok heeft uitgevoerd kunt u de taak
afvinken.<br>
De Radiobutton geeft aan met welke taak u momenteel bezig bent.<br><br>
</td>
</tr>
<tr BGCOLOR=#006699>
<td colspan="3"><FONT SIZE="2" FACE="Arial"
COLOR="#FFFFFF"><center><B>Taken Afvinken</B></center></font></td>
</tr>
<tr BGCOLOR=#FFD991>
<td valign="TOP">
<script language="javascript">
document.write('<br><FONT SIZE="2" FACE="Arial"> <br>');
document.write('<input type=radio name=first value=17><input
type=checkbox name=check17><font size=2 face="Arial">CH1</font><br>');
document.write('<input type=radio name=first value=0><input
type=checkbox name=check0><font size=2 face="Arial">CH2</font><br>');
document.write('<input type=radio name=first value=1><input
type=checkbox name=check1><font size=2 face="Arial">CH3</font><br>');
document.write('<input type=radio name=first value=20><input
type=checkbox name=check20><font size=2 face="Arial">CH4</font><br>');
document.write('<input type=radio name=first value=18><input
type=checkbox name=check18><font size=2 face="Arial">CH5</font><br>');
document.write('<input type=radio name=first value=2><input
type=checkbox name=check2><font size=2 face="Arial">CH6</font><br>');
document.write('<input type=radio name=first value=26><input
type=checkbox name=check26><font size=2 face="Arial">CH7</font><br>');
document.write('<input type=radio name=first value=23><input
type=checkbox name=check23><font size=2 face="Arial">CH8</font><br>');
document.write('<input type=radio name=first value=24><input
type=checkbox name=check24><font size=2 face="Arial">CH9</font><br>');
document.write('<input type=radio name=first value=30><input
type=checkbox name=check30><font size=2 face="Arial">CH10</font><br>');
document.write('<input type=radio name=first value=31><input
type=checkbox name=check31><font size=2 face="Arial">CH11</font><br>');
document.write('<br>');

document.write('<br></td><td valign="TOP">');
document.write('<br><FONT SIZE="2" FACE="Arial"> <br>');
document.write('<input type=radio name=first value=5><input
type=checkbox name=check5><font size=2 face="Arial">DF1</font><br>');
document.write('<input type=radio name=first value=6><input
type=checkbox name=check6><font size=2 face="Arial">DF2</font><br>');
document.write('<input type=radio name=first value=7><input
type=checkbox name=check7><font size=2 face="Arial">DF3</font><br>');
document.write('<input type=radio name=first value=12><input
type=checkbox name=check12><font size=2 face="Arial">DF4</font><br>');
document.write('<input type=radio name=first value=14><input
type=checkbox name=check14><font size=2 face="Arial">DF5</font><br>');
document.write('<input type=radio name=first value=15><input
type=checkbox name=check15><font size=2 face="Arial">DF6</font><br>');
document.write('<input type=radio name=first value=16><input
type=checkbox name=check16><font size=2 face="Arial">DF7</font><br>');

document.write('<input type=radio name=first value=3><input
type=checkbox name=check3><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=4><input
type=checkbox name=check4><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=19><input
type=checkbox name=check19><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=25><input
type=checkbox name=check25><font size=2 face="Arial"></font><br>');

document.write('<br></td><td valign="TOP">');
document.write('<br><FONT SIZE="2" FACE="Arial"> <br>');
document.write('<input type=radio name=first value=8><input
type=checkbox name=check8><font size=2 face="Arial">AI1</font><br>');
document.write('<input type=radio name=first value=21><input
type=checkbox name=check21><font size=2 face="Arial">AI2</font><br>');
document.write('<input type=radio name=first value=10><input
type=checkbox name=check10><font size=2 face="Arial">AI3</font><br>');
document.write('<input type=radio name=first value=27><input
type=checkbox name=check27><font size=2 face="Arial">AI4</font><br>');
document.write('<input type=radio name=first value=11><input
type=checkbox name=check11><font size=2 face="Arial">AI5</font><br>');
document.write('<input type=radio name=first value=9><input
type=checkbox name=check9><font size=2 face="Arial">AI6</font><br>');
document.write('<input type=radio name=first value=22><input
type=checkbox name=check22><font size=2 face="Arial">AI7</font><br>');
document.write('<input type=radio name=first value=13><input
type=checkbox name=check13><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=28><input
type=checkbox name=check28><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=29><input
type=checkbox name=check29><font size=2 face="Arial"></font><br>');
document.write('<input type=radio name=first value=32><input
type=checkbox name=check32><font size=2 face="Arial"></font><br>');

for(i=0;i<zmv.length;i++)
{
eval('document.zoekm.check'+zmv+'.checked=true');
}
aantalZm=document.zoekm.elements.length/2;

for(i=0;i<aantalZm;i++)
if
(zmv[0]==document.zoekm.first.value)document.zoekm.first.checked=true;
</script>
</td>
</tr>
<tr BGCOLOR=#FFD991><td colspan=6>&nbsp</td></tr>
<tr BGCOLOR=#FFD991><td colspan=6 align="center">
<input type="button" value="Opslaan" onClick="saveGegevens();">
<input type="button" value="Taken Leegmaken" onclick="doCheckboxes(
this.form, false );">
<input type="button" value="Alles Afvinken" onclick="doCheckboxes(
this.form, true );">
<input type=button value="Terug naar Smart Download"
onClick="top.location='index.htm'">
</td></tr>
</table>
</form>
</center>
</BODY>
</HTML>

================= CODE END ============================

RobG schreef:
Rob,

Thanks that works fine.
There is only one problem I have to deal with. When I uncheck
all boxes I have to click save to write the settings topm y cookie.
When I do so the checkbox next to the radiobutton that is checked
gets checked again.

Because your code tells it to be checked, see below.
function saveGegevens()
{
if(navigator.appName!="Microsoft Internet
Explorer"||parseInt(navigator.appVersion) >= 4){

I don't think this does anything useful. As noted previously, test for
support for features actually used - trying to guess what may or may not
be supported based on very unreliable browser detection is likely going
to cause far more issues that it solves (if it solves anything at all).

In the above test, some non-IE browser of version 1 (say Firefox) may
well be refused support, even though the features required to support
your script are trivial.

Because page developers insist on using flawed logic such as the test
above, browser vendors make their browsers report whatever appName or
appVersion they feel like. Many allow users set their browser to
masquerade as whatever they want.

Test for document.forms support, though I'd be surprised if any browser
in current use would fail.
zmv=new Array();
for(i=0; i<(aantalZm);i++)
{
if(document.zoekm.first.checked==true)
{ zmv[0]=document.zoekm.first.value;
eval('document.zoekm.check'+document.zoekm.first.value+'.checked=true')}
}



I thought we discussed 'eval' before. It is totally unnecessary, don't
use it. Also using global variables for counters is bad practice, the
brackets around aantalZm are unnecessary, you don't need to compare to
'true' explicitly and a local variable to store the value of
document.zoekm will make code simpler.

The line that is checking the checkbox is commented below:

function saveGegevens()
{
if { ! document.forms ) return;
var f = document.zoekm;
var zmv = [];
for( var i=0; i<aantalZm; i++ )
{
if ( f.first.checked )
{
zmv[0] = f.first.value;

// This line here is setting a checkbox to checked
f.check[ f.first.value ].checked = true;
}

}

var j=0;
for(i=0; i<aantalZm;i++)
{
if(eval('document.zoekm.elements['+((i*2)+1)+'].checked')==true
&& document.zoekm.first.value != zmv[0])
zmv[++j]=document.zoekm.first.value;
}


Get rid of eval:

var j=0;
for( i=0; i<aantalZm; i++ )
{
if ( f.elements[ ((i*2)+1) ].checked )
&& f.first.value != zmv[0])
{
zmv[++j] = f.first.value;
}
}
zmcv=zmv.join(';');
setCookie("zoek",zmcv);
top.location="index.htm";}
else window.alert("Het afvinken van taken niet mogelijk met de versie
van uw browser (3.0)");
}

If you have no further use for the global variable zmcv, it doesn't need
to be created:

setCookie( 'zoek', zmv.join(';') );
top.location = 'index.htm';
}
Can this code be changed so it doesn't do that?

Yes.


function saveGegevens()
{
// Test for support for forms collection
if { ! document.forms ) return;
var f = document.zoekm;
var zmv = [];
for( var i=0; i<aantalZm; i++ )
{
if ( f.first.checked )
{
zmv[0] = f.first.value;

// This line here is setting a checkbox to checked
f.check[ f.first.value ].checked = true;
}

}
var j=0;
for( i=0; i<aantalZm; i++ )
{
if ( f.elements[ ((i*2)+1) ].checked )
&& f.first.value != zmv[0])
{
zmv[++j] = f.first.value;
}
}
setCookie( 'zoek', zmv.join(';') );
top.location = 'index.htm';
}
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top