469,623 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Check all checkboxes

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[i].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

Jul 23 '05 #1
6 2571
te************@gmail.com wrote:
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[i].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:-

<URL: http://www.jibbering.com/faq/ >
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.
Jul 23 '05 #2
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
te************@gmail.com wrote:
This works fine -

More by coincidence than design given the - eval - use.


Cue:
<URL:http://www.pragmaticprogrammer.com/ppbook/extracts/coincidence.html>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
te************@gmail.com wrote:
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[i].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/archiv...t-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[i];
if ((e.type=='checkbox') && (!e.disabled)){
e.checked=true;
}
}
}

Mike
Jul 23 '05 #4
Thanks for the reply - you solved my problem, so much appreciated. Just
a few comments though:
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.


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

Jul 23 '05 #5
I was thinking exactly the same thing.

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

http://www.askblax.com

mscir wrote:
te************@gmail.com wrote:
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[i].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/archiv...t-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[i];
if ((e.type=='checkbox') && (!e.disabled)){
e.checked=true;
}
}
}

Mike


Jul 23 '05 #6
te************@gmail.com wrote:
<snip>
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.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Adrian Parker | last post: by
7 posts views Thread by Jaime Stuardo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.