...
> I am working on a web application which among other things uses DHTML, Java
> and Javascript.
When debugging JavaScript amd HTML, Java programmers
are the worst people to ask. Try..
comp.lang.javascript
Andrew T.
> I am working on a web application which among other things uses DHTML, Java
> and Javascript.
> It populates web page based on the contents of the database (resultset),
> and next to each row there is a checkbox (v1) allowing to select that row
> for changes (e.g. delete, update, etc.)
> So we are creating an array of checkbox, correct ?
"collection" is more appropriate as "array" implies a particular
programming entity which is not presented here.
<snip>
> Now it becomes strange.
> I have noticed that if there is 1 row [i.e. 1 checkbox v1], that function
[quoted text clipped - 10 lines]
> }
> Why ?
Because you are dealing here with the DOM 0 model for HTML forms and in
this model the internal call for a named form element is polymorphic:
If there is only one element with given name in given form, then a
reference to this element is returned. And HTML checkbox by default
doesn't have "length" property so it's properly reported as undefined.
If there are more than one element with given name in given form, then
a collection of all elements with this name is created and a reference
to this collection is returned. Respectively you have "length" property
set to the length of this collection.
To make your code correct, you have to make it ready to accept both an
element reference and a collection reference:
function isAnyChecked(check) {
var isChecked = false;
if ((typeof check.length == 'undefined)&&(check.checked)) {
isChecked = true;
}
else {
for(var i=0; i<check.length; i++) {
if (check[i].checked) {
isChecked = true;
break;
}
}
return isChecked;
}
> if(checkbox.length = "undefined") { // with single '=' !!!
That's a fancy construct, it shows how careful you have to be with
conditions check in JavaScript and suggests to learn this language
better :-)
So you are getting (as explained earlier) a form checkbox reference so
it doesn't have "length" property. But in JavaScript if you make an
assignment to a non-existing property, it simply creates new property
with the given name and with the assignment value.
So you just created new property "length" with string value
"undefined".
Assignment statements have their own return value: this is value of the
assignment result. So checkbox.length = "undefined" statement returns
string "undefined" and this is value thant if() block gets. But in
JavaScript any non-empty string in boolean checks is treated as true.
So effectively
if (checkbox.length = "undefined")
is equal to
if (true)
so the relevant branch is always executed. But in case of more than one
checkbox in the form is will produce run-time error because you are not
allowed to assign values to collection length, it's read-only.