Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2006

Tip: Looking for answers? Try searching our database.

Undefined and null in Javascript

Thread view: 
Oleg Konovalov - 29 Oct 2006 03:18 GMT
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.
Andrew Thompson - 29 Oct 2006 05:41 GMT
...
> 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.
VK - 29 Oct 2006 09:52 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.