471,896 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

Unexpectedly showing me.. Object is Undefined.

dmjpro
2,476 2GB
Let's see at my code fragment.
Expand|Select|Wrap|Line Numbers
  1. var checkBoxes = document.getElementsByTagName("input");
  2. for(var i=0;checkBoxes.length;i++){
  3.  if(checkBoxes[i].type.toLowerCase()=="checkbox" && checkBoxes[i].checked){
  4.  }
  5. }
  6.  
Tell me, how checkBoxes[i] is undefined?
What may be the cause? ;)
Apr 8 '09 #1
10 1635
Dormilich
8,658 Expert Mod 8TB
you've got a syntax error* in line 2.
Expand|Select|Wrap|Line Numbers
  1. for(var i=0; checkBoxes.length; i++)
must be
Expand|Select|Wrap|Line Numbers
  1. for(var i=0; i < checkBoxes.length; i++)
* technicly it is not an error, but it's not sensible either
Apr 8 '09 #2
dmjpro
2,476 2GB
S**** ... !! Anyway thanks!
Apr 8 '09 #3
Dormilich
8,658 Expert Mod 8TB
I've just had such a mistake myself.
Apr 8 '09 #4
gits
5,390 Expert Mod 4TB
just a note to speed such nodelist-iterations up ;) ... you could use it the following way:

Expand|Select|Wrap|Line Numbers
  1. var nodes = document.getElementsByTagName('input');
  2.  
  3. for (var i = 0, n; n = nodes[i]; ++i) {
  4.     if (n.type.toLowerCase() == 'checkbox' && n.checked) {
  5.         // checked checkbox
  6.     }
  7. }
kind regards
Apr 9 '09 #5
dmjpro
2,476 2GB
How does it speed up? In which sense you are telling? :(
Apr 9 '09 #6
Dormilich
8,658 Expert Mod 8TB
and then we're using while-loops, just because we can. *g*
Apr 9 '09 #7
Dormilich
8,658 Expert Mod 8TB
@dmjpro
I think because of n = nodes[i] (only one array access)
Apr 9 '09 #8
gits
5,390 Expert Mod 4TB
exactly, we don't retrieve the array's length and we just use one array-access per step ... when nodes[i] is undefined the loop terminates ...

kind regards
Apr 9 '09 #9
dmjpro
2,476 2GB
@Dormilich
What do you mean by one array access?

Here two things happen..
First it copies the reference then check whether it is defined or undefined or Null.

But what slows down in my previous one?
Apr 9 '09 #10
gits
5,390 Expert Mod 4TB
to use multiple checkBoxes[i] calls is not optimal ...
Apr 9 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Matt Sollars | last post: by
5 posts views Thread by Jon Paal | last post: by
reply views Thread by zermasroor | last post: by

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.