Connecting Tech Pros Worldwide Help | Site Map

Returning "null" or "false" if nextSibling doesn't exist

Member
 
Join Date: Feb 2008
Posts: 123
#1: Jul 25 '09
Hi all,

I have a routine that checks to see if an ID has been set for the next row down in a table. Everything works fine except if I'm on the last row and no row exists; the routine just hangs:

Expand|Select|Wrap|Line Numbers
  1. var checkNext = obj.parentNode.parentNode.nextSibling.id;
  2.  
Any ideas how I can get this to return "null" or "false" is there isn't a subsequent row in the table?

Many thanks in advance!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Jul 25 '09

re: Returning "null" or "false" if nextSibling doesn't exist


you can test for the next sibling first
Expand|Select|Wrap|Line Numbers
  1. if (obj.parentNode.parentNode.nextSibling)
  2. // or
  3. if ("undefined" == typeof obj.parentNode.parentNode.nextSibling)
Member
 
Join Date: Feb 2008
Posts: 123
#3: Jul 25 '09

re: Returning "null" or "false" if nextSibling doesn't exist


Works like a charm!

Thanks!

Quote:

Originally Posted by Dormilich View Post

you can test for the next sibling first

Expand|Select|Wrap|Line Numbers
  1. if (obj.parentNode.parentNode.nextSibling)
  2. // or
  3. if ("undefined" == typeof obj.parentNode.parentNode.nextSibling)

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Jul 25 '09

re: Returning "null" or "false" if nextSibling doesn't exist


you could do even more...
Expand|Select|Wrap|Line Numbers
  1. if (sibl = obj.parentNode.parentNode.nextSibling)
  2. {
  3.     var checkNext = sibl.id;
  4. }
  5. // ...
Reply