[Javascript] DOM navigation

Gee, David dgee at freemarkets.com
Wed Jun 2 13:54:35 CDT 2004



-----Original Message-----
From: Paul Novitski [mailto:paul at novitskisoftware.com] 

At 09:51 AM 6/2/2004, Gee, David wrote:
>  document.getElementById('myDiv').getElementsByTagName("TD")[0]

...In my more conservative moments 
I'd write this more safely as:

var oDiv = document.getElementById("myDiv")
         if (oDiv) var oCells = oDiv.getElementsByTagName("TD")
         if (oCells && oCells.length > 0) var oCell = oCells[0]

Cheers,
Paul 
-----End Original Message-----

Definitely true, but given that the first example was so closely tied to
the structure, and the request was specifically to get the 'column', it
seemed ok to just use the shorthand. But yep, I do this too often :)
If you're only targeting more modern browsers, wrapping the statement in
a try/catch block could be less cumbersome.

Also, what would the case be for oDiv.getElementsByTagName("TD")
returning a collection with length <= 0?

Cheers,
David




More information about the Javascript mailing list