473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Help with checking all related checkboxes

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
3 1394
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kate | last post by:
I have an PHP page that contains several checkboxes. I need to know if the user has check all of the checkboxes before they can proceed to the next page. How do I check there status? And can I...
3
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
2
by: Keith Bottner | last post by:
I just reinstalled my system and am in the process of getting PostgreSQL up and running again. During compilation of Postgres I received the following error: .... checking for main in...
8
by: linuxnooby | last post by:
Hi This is not worlking for me, I want all the checkboxes to be checked by default, what is wrong? <body onload="document.getElementsByTagName('input').checked=true"> cheers Dave
7
by: Dan | last post by:
(Using Classic ASP & MS Access) I have a page that has 120 fields on it (mostly checkboxes). I cannot split this into smaller pages. So what i want to do is write a class that handles this. in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
3
by: sakkat | last post by:
Hi, I should have a table with 2 columns in it, the first column will contain 10 checkboxes(the number is not fixed it changes dynamically) and a button(move) and the second column will be empty,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.