Hi,
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 ?
Of course I have to check whether any of these checkboxes exist and if any
of them got selected (checked)
using Javascript before allowing any DB operation in Java.
Now it becomes strange.
I have noticed that if there is 1 row [i.e. 1 checkbox v1], that function
always shows warning and returns false:
function isAnySelected(checkbox) //check if at least one checkbox is
selected
{
for(i=0; i<checkbox.length; i++) {
if(checkbox[i].checked) {
return true;
}
alert("You must choose at least one record.");
return false;
}
Why ? I call it as: onClick="if(! isAnySelected(v1)) return false;"
I tried to fix that error, and noticed that in the case of a single row
checkbox.length is "undefined".
So I modified that function into:
function isAnySelected(checkbox) //OK: check if at least one checkbox is
selected
{
if(checkbox.length = "undefined") { // with single '=' !!!
if(checkbox.checked) {
return true;
}
} else {
for(i=0; i<checkbox.length; i++) {
if(checkbox[i].checked) {
return true;
}
}
}
alert("You must choose at least one record.");
return false;
}
What I can't understand here is why that function works only when there is a
single '=' here:
if(checkbox.length = "undefined")... ???
Checking for null produces Javascript errors or does nothing.
And how can I make it work if there are 0 rows [and thus 0 instances of
checkbox v1 ?]
Thank you in advance,
Oleg.