checkboxes

S

Sjoerd

Hi all,

I have a form on which users can check several boxes. Long time ago I
added some javascript from another site to check all the boxes in one
column. I now found out that with more than ten columns, it doesn't
work: it seems that in case I check the 14th column, it checks the
first and the fourth also. Can anyone tell me how to change the code so
that it might work correctly?

function checkAll(theForm, cName, allNo_stat) {
var n=theForm.elements.length;
for (var i=0;i<n;i++){
if
(theForm.elements.className.indexOf(cName) !=-1){
if (allNo_stat.checked) {
theForm.elements.checked = true;
} else {
theForm.elements.checked = false;
}
}
}
}

In the table, above all columns I put this:
<input type='checkbox'
onclick=\"checkAll(document.getElementById('formname'), '{Number of
Column}',this);\" >

Thanks in advance for your help,

Sjoerd Mulder
 
R

RobG

Sjoerd said:
Hi all,

I have a form on which users can check several boxes. Long time ago I
added some javascript from another site to check all the boxes in one
column. I now found out that with more than ten columns, it doesn't
work: it seems that in case I check the 14th column, it checks the
first and the fourth also. Can anyone tell me how to change the code so
that it might work correctly?

It seems that the script wants all the checkboxes in a column to have
the column number as a class name.

function checkAll(theForm, cName, allNo_stat) {
var n = theForm.elements.length;
for (var i=0; i<n; i++){
if (theForm.elements.className.indexOf(cName) != -1){
if (allNo_stat.checked) {
theForm.elements.checked = true;
} else {
theForm.elements.checked = false;
}
}



Replace with:

function checkAll(theForm, cName, chk)
{
var n = theForm.elements.length;

// This will match only the whole word of the classname
var re = new RegExp('\\b' + cName + '\\b');
var el;

for (var i=0; i<n; i++){
el = theForm.elements;
if ( re.test(el.className) ){
el.checked = chk;
}
}
}

}
}

In the table, above all columns I put this:
<input type='checkbox'
onclick=\"checkAll(document.getElementById('formname'), '{Number of

Give the form a name of 'formname' and use (wrapped for posting):

onclick="checkAll(document.forms['formname'],
'colNumber', this.checked);"


Where colNumber is the column number. The forms collection is more
widely supported than getElementById. Whether the checkbox is checked
or not is passed directly to the function.


[...]
 
T

Thomas 'PointedEars' Lahn

Sjoerd said:
I have a form on which users can check several boxes. Long time ago I
added some javascript from another site to check all the boxes in one
column. I now found out that with more than ten columns, it doesn't
work: it seems that in case I check the 14th column, it checks the
first and the fourth also. Can anyone tell me how to change the code so
that it might work correctly?

I do not see anything syntactically or semantically wrong with the code,
[...]
In the table, above all columns I put this:
<input type='checkbox'
onclick=\"checkAll(document.getElementById('formname'), '{Number of
Column}',this);\" >

you just need to make sure that you serve Valid HTML and CSS, that you pass
the correct number to it and use the correct `class' attribute value.
However, I strongly suggest you use the following less error-prone and more
efficient instead:

function checkAll(theForm, cName, allNo_stat)
{
var rx = new RegExp("\\b" + cName + "\\b"), c = allNo_stat.checked;
for (var els = theForm.elements, i = rx && els && els.length; i--;)
{
var o = els;

if (o.type == "checkbox" && rx.test(o.className))
{
o.checked = c;
}
}
}

<input type='checkbox'
onclick="checkAll(this.form, 'c14', this);">

Also note that CSS class names are invalid if they contain only numbers.

<URL:http://validator.w3.org/>
<URL:http://jigsaw.w3.org/css-validator/>

You might want to consider using the `name' attribute instead, that is,
all checkboxes of one group get the same name; or consider using a
parent `fieldset' element common to all checkboxes of one group.


HTH

PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 23 Dec
2005 13:18:27 local, seen in Thomas
'PointedEars' Lahn said:
I do not see anything syntactically or semantically wrong with the code,

Well, we already knew that you are not an intelligent programmer, merely
a technician.

Given that fourteen is well-known to be written as one four, it's
obvious that the code is checking for the presence of a character rather
than the presence of a number, and so finding both one and four in
fourteen. That'll be in the first if statement.



OP : you need to be more selective about what you take from the Web;
it's riddled with bad code.

Don't copy from anyone who puts such as

if (allNo_stat.checked) {
theForm.elements.checked = true;
} else {
theForm.elements.checked = false;

instead of using just

theForm.elements.checked = allNo_stat.checked ;

since he/she is evidently feeble-minded.
 
R

RobG

Jasen said:
you're using it incorrectly, pass it an array.

It (the function) is being used 'correctly', though beyond its design
limits. It will only work as the OP requires where the class number
is 0-9 inclusive.

It doesn't need to be passed an array, the elements collection that it
already uses is sufficient. The problem is in how the className is
matched with the column number passed to the function.

<input type='checkbox'
onclick=\"checkAll(document.getElementById('formname'), '{Number of
Column}',this);\" >


to check the fourteenth,

onclick="checkAll(document.getElementById('formname'), [14],this);"

I think you missed what's happening. :) It appears that all the
checkboxes in a column are given the column number as a class name.
The OP doesn't want to check just the 14th, but all the checkboxes in
the 14th column.


[...]
also it should be type="checkbox" not type='checkbox' but that may not
make much difference.


Absolutely none. It is common practice to use double quotes, but HTML
is quite happy with either double or single quotes around attribute
values:

<URL: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 >


However the OP's use of quoted quotes is not appropriate:

onclick=\"checkAll(...);\"


should be:

onclick="checkAll(...);"
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted :
I do not see anything syntactically or semantically wrong with the code,

Well, we already knew that you are not an intelligent programmer,

How dare you speak for all people, where many have been thankful for
insights and working code based on that I have provided over the years!
merely a technician.

I do not want to repeat here and especially not today what other hateful
things I have had to read from you here over time. Everybody is free to
use the archives and compare for themselves.
Given that fourteen is well-known to be written as one four, it's
obvious that the code is checking for the presence of a character rather
than the presence of a number, and so finding both one and four in
fourteen. That'll be in the first if statement.

You missed the point completely.

Given that this is about _checkboxes_, the element object it is represented
by it has the boolean `checkbox' property. What the OP (or the author of
that code) did not notice was that not every form control element object
has that property.

Which checkboxes should be checked, is tested for with the className
property representing the value of the `class' attribute of elements.
What you correctly pointed out unintentionally is that the _Regular
Expression_ both RobG and I have provided is not suited to solve this
particular problem, since this is about substrings in class names;
but that was not what was the target of your "criticism".
OP : you need to be more selective about what you take from the Web;
it's riddled with bad code.

True, but in case you did not notice, we are not on the Web here. This is
NetNews.

OP: Do not use the Regular Expression that was provided; you may continue
to use indexOf(). The rest of the code, however, should be changed.
Don't copy from anyone who puts such as

if (allNo_stat.checked) {
theForm.elements.checked = true;
} else {
theForm.elements.checked = false;


If `theForm.elements' represents not a HTMLInputElement here, which is
possible since no previous test on that was done, the object referred to
is either added an unused property or an error occurs.
instead of using just

theForm.elements.checked = allNo_stat.checked ;


You have not noticed or deliberately omitted that I included a previous
test that theForm.elements represents a checkbox, so with that, the
above _is_ semantically equal. allNo_stat is a reference to a
Checkbox/HTMLInputElement object that has the boolean `checked' property
which value can either be `true' or `false'; the IfExpression in the
original code checks for nothing more.
since he/she is evidently feeble-minded.

("Humbug, I tell you; humbug!")

I wish you a lonely Christmas, Scrooge.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 24 Dec
2005 12:42:58 local, seen in Thomas
'PointedEars' Lahn said:
Dr John Stockton wrote:

True, but in case you did not notice, we are not on the Web here. This is
NetNews.

OP, as you should know, stands for Original Poster or Original Posting.
I was not addressing you; mind your own business. The OP stated that
the code came from another site.

Don't copy from anyone who puts such as

if (allNo_stat.checked) {
theForm.elements.checked = true;
} else {
theForm.elements.checked = false;
instead of using just

theForm.elements.checked = allNo_stat.checked ;


You have not noticed or deliberately omitted that I included a previous
test that theForm.elements represents a checkbox, so with that, the
above _is_ semantically equal.


Of course it's semantically equal; but, as I indicated, it is
incompetent coding.
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 12/24/2005 6:42 AM:
Dr John Stockton wrote:

[...] Thomas 'PointedEars' Lahn [...] posted :
Sjoerd wrote:

I have a form on which users can check several boxes. Long time ago I
added some javascript from another site to check all the boxes in one
column. I now found out that with more than ten columns, it doesn't
work: it seems that in case I check the 14th column, it checks the
first and the fourth also. Can anyone tell me how to change the code so
that it might work correctly?

I do not see anything syntactically or semantically wrong with the code,

Well, we already knew that you are not an intelligent programmer,


How dare you speak for all people, where many have been thankful for
insights and working code based on that I have provided over the years!

If he does not speak "for all people" then he speaks for the one's of us
that have read your past ramblings/garbage. But for what it's worth, the
statement he made can be confirmed by anyone with enough common sense to
research your postings in this group - they speak for themselves.

As for people being thankful, the only one's that are thankful for
anything you post are people who know no better.
 

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
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top