473,405 Members | 2,373 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,405 software developers and data experts.

Check checkbox strange problem

Dear all,

I have a program that creates dinamically a web page.
In the page I have the following function to check how many checkbox
are checked.
function tarInfo(info) {
var i=0;
var c=0;
var l=document.forms[0].selected_files.length;
alert(l);
for (i=0; i<document.forms[0].selected_files.length;i++) {
if (document.forms[0].selected_files[i].checked) {
c++
}
}
..
..

The problem is that if I have only one checkbox, the alert(l) write
undefined. If I have more than 2 checkbox, the function works well.
Here a part of the webpage with one checkbox.

<form name="form1" action="filemanager" method="POST"
enctype="multipart/form-data" >
..
..
<td>
<input type="checkbox" value="pbsrun.o100"
name="selected_files">
</td>
..
..
<input type="submit" class="button" name="tar" value="Tar"
onClick="return tarInfo('tar')">

Where is the problem?
Thanks
Enrico

Dec 27 '06 #1
9 1775
morellik wrote:
I have a program that creates dinamically a web page.
In the page I have the following function to check how many checkbox
are checked.
function tarInfo(info) {
var i=0;
var c=0;
var l=document.forms[0].selected_files.length;
alert(l);
for (i=0; i<document.forms[0].selected_files.length;i++) {
if (document.forms[0].selected_files[i].checked) {
c++
}
}
The problem is that if I have only one checkbox, the alert(l) write
undefined. If I have more than 2 checkbox, the function works well.
[...]
I think you have a small misunderstanding of how <input
type="checkbox"works. You can't have more than one <input
type="checkbox"with the same name-attribute in the same form, because
each checkbox is an object of its own (unlike <input type="radio">). It
would be just the same as saying:

<input type="text" name="password" value="abc">
<input type="text" name="password" value="123">

It's unpredictable how browsers react on this kind of constructions
when submitting the form or when scripting such elements. Changing your
HTML-code so that no 2 <input type="checkbox">'s hold the same name,
will probably solve your problem. Once that is okay, one can obtain the
number of checked boxes by doing:

<form>
<input type="checkbox" value="v1" name="c1">
<input type="checkbox" value="v2" name="c2">
<input type="checkbox" value="v3" name="c3">
<input type="checkbox" value="v4" name="c4">
<input type="button" value="check" onClick="check_boxes()">
</form>
<script type="text/javascript">
function check_boxes() {
var c = 0
for (var i=0; i<document.forms[0].length; i++) {
if (document.forms[0].elements[i].type =='checkbox') {
if (document.forms[0].elements[i].checked) {
c++
}
}
}
alert(c + ' checkboxes are checked in forms[0]')
}
</script>

Hope this helps,

--
Bart

Dec 27 '06 #2
Lee
morellik said:
>The problem is that if I have only one checkbox, the alert(l) write
undefined. If I have more than 2 checkbox, the function works well.
If there is only one checkbox, the form control is not an array,
and so has no length attribute. There are several possible ways
to handle this. You could check to see if the length is undefined
or, since you are generating the page, you could generate different
validation code depending on the number of checkboxes.
--

Dec 27 '06 #3
ASM
morellik a écrit :
Dear all,

I have a program that creates dinamically a web page.
In the page I have the following function to check how many checkbox
are checked.
function tarInfo(info) {
var i=0;
var c=0;
var l = document.forms[0].selected_files;
l = l.length? l.length : 0;
alert(l);
if(l>0)
for (i=0; i<document.forms[0].selected_files.length;i++)
if (document.forms[0].selected_files[i].checked) c++;
else
if (document.forms[0].selected_files.checked) c=1;
}
The problem is that if I have only one checkbox, the alert(l) write
undefined. If I have more than 2 checkbox, the function works well.
Other way :

function tarInfo(info)
{
var c=0;
var l = info.elements;
for(var i=0; i<l.length; i++)
if(
l[i].type=='checkbox' &&
l[i].name.indexOf('selected_files')==0 &&
l[i].checked
)
c++;
return confirm('You have selected : '+c+' file(s)\nIs it OK?');
}

<form name="form1" action="filemanager" method="POST"
enctype="multipart/form-data" onsubmit="return tarInfo(this);">

<p>File 0100 :
<input name="selected_files[0]" type="checkbox" value="pbsrun.o100">
<p>File 0200 :
<input name="selected_files[1]" type="checkbox" value="pbsrun.o200">
<p>File 0300 :
<input name="selected_files[2]" type="checkbox" value="pbsrun.o300">
<p>File 0400 :
<input name="selected_files[3]" type="checkbox" value="pbsrun.o400">

<input type="submit" class="button" name="tar" value="Tar">
</form>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 27 '06 #4
Lee
Bart Van der Donck said:
>
morellik wrote:
>I have a program that creates dinamically a web page.
In the page I have the following function to check how many checkbox
are checked.
function tarInfo(info) {
var i=0;
var c=0;
var l=document.forms[0].selected_files.length;
alert(l);
for (i=0; i<document.forms[0].selected_files.length;i++) {
if (document.forms[0].selected_files[i].checked) {
c++
}
}
The problem is that if I have only one checkbox, the alert(l) write
undefined. If I have more than 2 checkbox, the function works well.
[...]

I think you have a small misunderstanding of how <input
type="checkbox"works. You can't have more than one <input
type="checkbox"with the same name-attribute in the same form, because
each checkbox is an object of its own (unlike <input type="radio">).
The understanding is yours.
Form controls that share the same NAME attribute are treated as an array.
--

Dec 27 '06 #5
ASM wrote:
function tarInfo(info)
{
var c=0;
var l = info.elements;
for(var i=0; i<l.length; i++)
if(
l[i].type=='checkbox' &&
l[i].name.indexOf('selected_files')==0 &&
l[i].checked
)
c++;
return confirm('You have selected : '+c+' file(s)\nIs it OK?');
}

<form name="form1" action="filemanager" method="POST"
enctype="multipart/form-data" onsubmit="return tarInfo(this);">

<p>File 0100 :
<input name="selected_files[0]" type="checkbox" value="pbsrun.o100">
<p>File 0200 :
<input name="selected_files[1]" type="checkbox" value="pbsrun.o200">
<p>File 0300 :
<input name="selected_files[2]" type="checkbox" value="pbsrun.o300">
<p>File 0400 :
<input name="selected_files[3]" type="checkbox" value="pbsrun.o400">

<input type="submit" class="button" name="tar" value="Tar">
</form>
Your code is rather infirm IMHO. By naming your checkboxes
selected_files[0] to selected_files[3], you seem to create the
impression that they belong to a same array, which is not the case. I
would even counsel to use only alphanumericals (+underscore) as
elements names in the first place. Also, I think the line where you say
l[i].checked is to be avoided. Only checkboxes can be ".checked" or
not, but all other form elements pass your for-loop as well. But yes,
IE & FF do not seem to bark at that (though I had expected they would
have).

--
Bart

Dec 27 '06 #6
ASM
Bart Van der Donck a écrit :
ASM wrote:
>function tarInfo(info)
{
var c=0;
var l = info.elements;
for(var i=0; i<l.length; i++)
if(
l[i].type=='checkbox' &&
l[i].name.indexOf('selected_files')==0 &&
l[i].checked
)
c++;
return confirm('You have selected : '+c+' file(s)\nIs it OK?');
}

<form name="form1" action="filemanager" method="POST"
enctype="multipart/form-data" onsubmit="return tarInfo(this);">

<p>File 0100 :
<input name="selected_files[0]" type="checkbox" value="pbsrun.o100">
<p>File 0200 :
<input name="selected_files[1]" type="checkbox" value="pbsrun.o200">
<p>File 0300 :
<input name="selected_files[2]" type="checkbox" value="pbsrun.o300">

<input type="submit" class="button" name="tar" value="Tar">
</form>

Your code is rather infirm IMHO. By naming your checkboxes
selected_files[0] to selected_files[3], you seem to create the
impression that they belong to a same array, which is not the case.
I did see that after having posted, it is not very important in what I
wanted to show, OP can even keep the unique name he has chosen.
Also, I think the line where you say
l[i].checked is to be avoided.
Isn't it what we search ? (checked checkboxes)
Only checkboxes can be ".checked" or
not,
and radio-buttons too :-)
but all other form elements pass your for-loop as well. But yes,
IE & FF do not seem to bark at that (though I had expected they would
have).
Of course :
as soon as
l[i].type=='checkbox'
returns false, next conditions aren't read by JS parser.

if it returns true
then we see if the checkbox name begins with correct string
then we verify if it is checked
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 27 '06 #7
Lee wrote:
Bart Van der Donck said:
I think you have a small misunderstanding of how <input
type="checkbox"works. You can't have more than one <input
type="checkbox"with the same name-attribute in the same form, because
each checkbox is an object of its own (unlike <input type="radio">).

The understanding is yours.
Form controls that share the same NAME attribute are treated as an array.
You're right.

var d = 0;
if (document.forms[0].elements['c'].length)
{
for (var i=0; i<document.forms[0].elements['c'].length; ++i) {
if (document.forms[0].elements['c'][i].checked) ++d;
}
}
else
{
if (document.forms[0].elements['c'].checked) ++d;
}
alert(d + ' checkboxes are checked in forms[0]')

should always work (for single/multiple checkboxes named "c" in this
case).

--
Bart

Dec 27 '06 #8
ASM wrote:
Bart Van der Donck a écrit :
ASM wrote:
function tarInfo(info)
{
var c=0;
var l = info.elements;
for(var i=0; i<l.length; i++)
if(
l[i].type=='checkbox' &&
l[i].name.indexOf('selected_files')==0 &&
l[i].checked
)
c++;
return confirm('You have selected : '+c+' file(s)\nIs it OK?');
}

<form name="form1" action="filemanager" method="POST"
enctype="multipart/form-data" onsubmit="return tarInfo(this);">

<p>File 0100 :
<input name="selected_files[0]" type="checkbox" value="pbsrun.o100">
<p>File 0200 :
<input name="selected_files[1]" type="checkbox" value="pbsrun.o200">
<p>File 0300 :
<input name="selected_files[2]" type="checkbox" value="pbsrun.o300">

<input type="submit" class="button" name="tar" value="Tar">
</form>
Your code is rather infirm IMHO. By naming your checkboxes
selected_files[0] to selected_files[3], you seem to create the
impression that they belong to a same array, which is not the case.

I did see that after having posted, it is not very important in what I
wanted to show, OP can even keep the unique name he has chosen.
Yes, but one could easily get into trouble with constructions like
document.forms[0].selected_files[0].value
(with selected_files[0] being thought of as the actual name-argument,
for instance)
Also, I think the line where you say
l[i].checked is to be avoided.

Isn't it what we search ? (checked checkboxes)
Yes (see below).
Only checkboxes can be ".checked" or
not,

and radio-buttons too :-)
Right. I thought of ".selected" for radios when I wrote that.
but all other form elements pass your for-loop as well. But yes,
IE & FF do not seem to bark at that (though I had expected they would
have).

Of course :
as soon as
l[i].type=='checkbox'
returns false, next conditions aren't read by JS parser.

if it returns true
then we see if the checkbox name begins with correct string
then we verify if it is checked
Indeed, now I remember again that the circuit is left at the first
non-match with '&&' and at the first match with '||'. Didn't think that
far!

--
Bart

Dec 27 '06 #9
Bart Van der Donck wrote:
ASM wrote:
Bart Van der Donck a écrit :
Only checkboxes can be ".checked" or
not,
and radio-buttons too :-)

Right. I thought of ".selected" for radios when I wrote that.
Some further thoughts about this obvious mistake. Actually I think that
".selected" would be a more intuitive syntax for radios than
".checked". The former seems to involve a sort of list where one can
pick out an item, like chosen from a group such as e.g. a person that
is selected for a job so. The latter makes me rather think of a boolean
flag (yes/no) on a strict per-element base that has nothing to do with
anything else in the form. Of course each radio can be thought of as
boolean as well, but not in the same sense as a checkbox (which is
"more boolean" in that regard). Therefore I believe that radios have
more in common with <select>s than with checkboxes. In that view, it
would be linguistically more logic to think of ".selected" rather than
".checked" for radios.

--
Bart

Jan 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rich | last post by:
I have a form with 2 check boxes. One of the check boxes is used to specify that the user is a "primary contact." When I check the primary contact box I want a second box for "standard contact"...
4
by: welie | last post by:
I have a problem canceling a check box update when placing a check in it. Checkbox is not bound. Here is what happens. User clicks a check box. In the BeforeUpdate method of the control, if...
4
by: SJ | last post by:
Hi all, I have come across a weird problem when attempting to automatically set the focus in a vb.net form to a checkbox control... In my form I have (on a tab page in a tab control) several...
2
by: limbo2u | last post by:
I'm using the following code for a checkbox that when clicked, either checks or unchecks a group of checkboxes on a form. The code works fine, except when there is only one checkbox in the group in...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
0
by: Deepson Thomas | last post by:
Hi, I met with a strange problem in my web form. At least this is something new or strange for me. Okay let me describe the situation. I have a text box and a checkbox on the right side of the...
4
by: peter | last post by:
I've come across a weird difference between the behaviour of the Tkinter checkbox in Windows and Linux. The issue became apparent in some code I wrote to display an image in a fixed size canvas...
4
by: it2051229 | last post by:
Well i'm having a problem with the compatibility of javascript and PHP multiple delete check box.. i used a javascript for the "CHECK ALL BOXES" just like yahoo mail.. so my input is something like...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.