[Javascript] DOM navigation

Flavio Gomes flavio at economisa.com.br
Wed Jun 2 15:14:53 CDT 2004


Gee, David wrote:

>-----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?
>  
>
  When there are no "<td>"  oCells.length sould return 'undefined', and 
oCells.length is nor bigger, nor smaller than zero, so I believe that  
the idea is only to return a false boolean to the && operator.

---
Flavio Gomes
flavio at economisa.com.br






More information about the Javascript mailing list