By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

Help with checking all related checkboxes

P: n/a
I have this working, but I don't think it's done efficiently or the best
way:

<a href="javascript:setAllAccounts(true)">check all</a>
<a href="javascript:setAllAccounts(false)">clear all</a>

function setAllAccounts(value) {
for(i = 0 ; i < document.forms[0].accounts.length; i++ ) {
document.forms[0].accounts[i].checked = value; } }

And the form has:
<input type="checkbox" name="includeDetail" value="on">
<input type="checkbox" name="accounts" value="99_A_ABCD1234">
<input type="checkbox" name="accounts" value="88_B_EFGH5678">

So it checks only the 'accounts' checkboxes, and leaves the 'includeDetail'
one alone.

First, I don't think the javascript: in the href is recommended. And
second, could I have a function:
setAll( fieldname, value)
instead? If so, how would I do the for loop? My initial attempts just
produced errors.

Thanks,
Wendy in Chandler, AZ
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <c1**********@news.asu.edu>, we******@hotmail.com enlightened us with...
I have this working, but I don't think it's done efficiently or the best
way:

<a href="javascript:setAllAccounts(true)">check all</a>
Eeek!
<a href="someDefaultActionPageForPeopleWithNoScript.h tml"
onClick="setAllAccounts(true);return false;">check all</a>

function setAllAccounts(value) {
for(i = 0 ; i < document.forms[0].accounts.length; i++ )
Eek!
IE syntax.
Try
for(i = 0 ; i < document.forms[0].elements["accounts"].length; i++ )

First, I don't think the javascript: in the href is recommended. And
second, could I have a function:
setAll( fieldname, value)
instead?


Sure.
function setAllAccounts(e,value) {
for(i = 0 ; i < document.forms[0].elements[e].length; i++ ) {
document.forms[0].elements[e][i].checked = value; } }

Note: this is inefficient due to repeated evaluations of the length attribute.
To make it even better...
function setAllAccounts(e,value)
{
l = document.forms[0].elements[e].length;
for(i = 0 ; i < l; i++ )
{
document.forms[0].elements[e][i].checked = value;
}
}

<input type="checkbox" name="myElementName">
....

<a href="someDefaultActionPageForPeopleWithNoScript.h tml"
onClick="setAllAccounts('myElementName',true);retu rn false;">check all</a>

Another note: to be production quality, the script should check that the element is indeed:
1. part of an array
2. a checkbox (because you use checked property)
--
--
~kaeli~
I do whatever my Rice Krispies tell me to.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2

P: n/a
kaeli wrote:
In article <c1**********@news.asu.edu>, we******@hotmail.com enlightened us with...
Please shorten your attribution (by removing superfluid information)
so that it does not exceed the practical 78 columns limit as recommended
by RFCs.
function setAllAccounts(value) {
for(i = 0 ; i < document.forms[0].accounts.length; i++ )


Eek!
IE syntax.


No, DOM Level 0 object references and thus perfectly
working in Mozilla, Opera and the like. However,
for(i = 0 ; i < document.forms[0].elements["accounts"].length; i++ )
is indeed better since it is standards-compliant (W3C-DOM Level 2 HTML).
function setAllAccounts(e,value) {
for(i = 0 ; i < document.forms[0].elements[e].length; i++ ) {
document.forms[0].elements[e][i].checked = value; } }

Note: this is inefficient due to repeated evaluations of the length attribute.
_Property_, not attribute. And your code
allows for improvement in Pretty Printing.
To make it even better...
function setAllAccounts(e,value)
{
l = document.forms[0].elements[e].length; ^^^^ for(i = 0 ; i < l; i++ ) ^^ {
document.forms[0].elements[e][i].checked = value; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ }
}
Looking up the rest again and again is inefficient as well,
if not more. Besides, "l" and "i" are declared _global_.
Use this instead:

var es = document.forms[0].elements[e];
if (es)
{
var l = es.length;

for (var i = 0; i < l; i++)
{
es[i].checked = value;
}
}

Performance nevertheless can still be improved by passing
a reference to the "form" element object instead:

function setAllAccounts(oForm, e, value)
{
if (oForm
&& typeof oForm.elements != "undefined")
&& typeof oForm.elements[e] != "undefined")
{
var es = oForm.elements[e];

// ...
}
}

... setAllAccounts(this.form, 'whatever', true); ...
<input type="checkbox" name="myElementName">
....

<a href="someDefaultActionPageForPeopleWithNoScript.h tml"
onClick="setAllAccounts('myElementName',true);retu rn false;">check all</a>
I would rather use a checkbox indicating that all items are meant and
use its "onclick" handler to check the checkboxes client-side. If
client-side scripting is not present, too much restricted or disabled,
the server-side application then can retrieve the submitted value and
work anyway. The WEB.DE web mail interface I am using takes advantage
of such an implementation.
Another note: to be production quality, the script should check that the element is indeed:
Please wrap your lines before the 80th column (76 is OK, 72 is
recommended to help for reading and making quotations).
1. part of an array
Collection, not array. As such, it may be difficult to determine the
described property, since the "length" property is not restricted to
HTMLCollection and Array objects. However, checkboxes within forms
have been part of collections since DOM 0, so there is practically no
need to check for this. What needs to be checked for is that the
HTMLInputElement object exists, before
2. a checkbox (because you use checked property)


this check should take place. BTW, radiobutton element objects also
have a functional "checked" property.
PointedEars
Jul 20 '05 #3

P: n/a
JRS: In article <40**************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Sat, 28 Feb 2004 23:55:54 :-
kaeli wrote:
In article <c1**********@news.asu.edu>, we******@hotmail.com enlightened us

with...

Please shorten your attribution (by removing superfluid information)
so that it does not exceed the practical 78 columns limit as recommended
by RFCs.


There is no such recommendation as to the length of an attribution.

Moreover, please remember that you are not a native English speaker, and
use a good dictionary to determine correct usage of long words, in order
not to mislead other foreigners.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.