Check all checkboxes

T

terence.parker

I currently have the following JS in my header:

function checkall(thestate) {
var checkboxes=eval("document.forms.EssayList.file_id")
for (i=0;i<checkboxes.length;i++)
checkboxes.checked=thestate
}


This works fine - but the problem is that it doesn't work with my
checkboxes when they are set to name="file_id[]" . I have tried without
the parenthesis, and that works fine - so I guess that's the problem.
However I do need my POST to submit an array. (As a side question... is
there even a point using checkboxes but not submitting an array? If you
can only submit one item... why not use radio?) .

Is there a way around my problem?

Thanks,

Terence
 
R

Richard Cornford

I currently have the following JS in my header:

function checkall(thestate) {
var checkboxes=eval("document.forms.EssayList.file_id")

I nominate this line for this month's "Most futile use of eval" award.
for (i=0;i<checkboxes.length;i++)
^
Using global variables as loop counters is an extremely bad habit to get
into.
checkboxes.checked=thestate
}


This works fine -


More by coincidence than design given the - eval - use.
but the problem is that it doesn't work with my
checkboxes when they are set to name="file_id[]" .

No it wouldn't. The item to the right of a dot in a dot notation
property accessor must be an Identifier, and so only a limited set of
character sequences may be used. The sequence file_id[] not only is not
in that set of character sequences but you cannot place it, exposed, in
javascript source code at all without producing a syntax error. Of
course you can put (almost) any sequence of characters in a string
literal without any consideration of its syntactic correctness as
javascript source code (at least so long as you don't show it to the -
eval - function). And because you can use "file_id[]" in a string, and
can use strings as property names in bracket notation property
accessors, you can avoid the problem of the element's name not being a
valid javascript Identifier:-

function checkAll(state){
var col = document.forms.EssayList.elemetns['file_id[]'];
for(var c = col.length;c--;){
col[c].checked = state;
}
}

- Though it would be better to perform the feature testing required to
render the use of that function safe, at some point prior to the use of
the function.

Generally, see:-

I have tried without the parenthesis, and that
works fine - so I guess that's the problem.

No need to guess, look at the error reports generated (though - eval -
is a little inclined to mask errors).

However I do need my POST to submit an array. (As a side
question... is there even a point using checkboxes but not
submitting an array? If you can only submit one item...
why not use radio?) .

You are misperceiving the relationship between an HTTP POST request and
what your server-side technology (PHP?) does to interpret that request.
HTTP POST requests have no concept of 'array'. All successful from
controls have their names and values included in the HTTP POST request,
so all checked checkboxes result in name/value pairs being sent. And
multiple same-named checkboxes contribute multiple name/value pairs with
the same name, and (theoretically, but not necessarily) different
values. If your server side technology dose not expose that data to your
code when it arrives then that is a limitation in the server-side
technology (or, more likely, your understanding of it).

Richard.
 
M

mscir

I currently have the following JS in my header:

function checkall(thestate) {
var checkboxes=eval("document.forms.EssayList.file_id")
for (i=0;i<checkboxes.length;i++)
checkboxes.checked=thestate
}


This works fine - but the problem is that it doesn't work with my
checkboxes when they are set to name="file_id[]" . I have tried without
the parenthesis, and that works fine - so I guess that's the problem.
However I do need my POST to submit an array. (As a side question... is
there even a point using checkboxes but not submitting an array? If you
can only submit one item... why not use radio?) .

Is there a way around my problem?
Thanks,
Terence


http://forums.devarticles.com/archive/t-11087/Select-all-checkbox

How about checking all of the checkboxes in the form:

function checkall(frm){
for (var i=0; i<document.forms[frm].elements.length; i++) {
var e=document.forms[frm].elements;
if ((e.type=='checkbox') && (!e.disabled)){
e.checked=true;
}
}
}

Mike
 
T

terence.parker

Thanks for the reply - you solved my problem, so much appreciated. Just
a few comments though:
I nominate this line for this month's "Most futile use of eval" award.

Thanks for the tip. However, as you correctly identified at the end of
my post I was indeed using PHP - and since i'm learning the languages
one at a time I know near to nothing about JS at the moment : this
script was nabbed off javascriptkit.com . In fact this is the second
script I have taken from there, written about, then received replies
saying how bad it was - so I guess now I know about the quality of the
JS scripts from that site!
Using global variables as loop counters is an extremely bad habit to get
into.

As with the above, I had no idea, but when I do come to learn JS i'll
keep that tip in mind.
No it wouldn't. The item to the right of a dot in a dot notation
property accessor must be an Identifier, and so only a limited set of
character sequences may be used. -snip-

function checkAll(state){
var col = document.forms.EssayList.elements['file_id[]'];
for(var c = col.length;c--;){
col[c].checked = state;
}
}

Thanks for that - you did manage to solve my problem there so all is
well now.

You are misperceiving the relationship between an HTTP POST request and
what your server-side technology (PHP?) does to interpret that request. -snip-

Yup... you're right ; how silly of me, I wasn't thinking. As far as I
gather though, PHP does require the form data to be submitted with the
variable name as [] so that it enters an array when the PHP is
executed. This is since even if I request a printout of the raw form
POST it would only contain one field if I didn't use the parenthesis.

What languages accept the form POST to be without the [] but yet retain
the data when executed? Just curious.

Thanks again,

Terence
 
A

askMe

I was thinking exactly the same thing.

if ((e.type=='checkbox') && (!e.disabled)){
e.checked=true;
}

http://www.askblax.com
I currently have the following JS in my header:

function checkall(thestate) {
var checkboxes=eval("document.forms.EssayList.file_id")
for (i=0;i<checkboxes.length;i++)
checkboxes.checked=thestate
}


This works fine - but the problem is that it doesn't work with my
checkboxes when they are set to name="file_id[]" . I have tried without
the parenthesis, and that works fine - so I guess that's the problem.
However I do need my POST to submit an array. (As a side question... is
there even a point using checkboxes but not submitting an array? If you
can only submit one item... why not use radio?) .

Is there a way around my problem?
Thanks,
Terence


http://forums.devarticles.com/archive/t-11087/Select-all-checkbox

How about checking all of the checkboxes in the form:

function checkall(frm){
for (var i=0; i<document.forms[frm].elements.length; i++) {
var e=document.forms[frm].elements;
if ((e.type=='checkbox') && (!e.disabled)){
e.checked=true;
}
}
}

Mike
 
R

Richard Cornford

What languages accept the form POST to be without
the [] but yet retain the data when executed?
Just curious.

In Java the - getParameterValues - method of the - ServletRequest -
interface returns an array of all of the values from same-name
name/value pairs in an HTTP request without any restriction on the names
used.

Richard.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top