[Javascript] Code optimization

Terry Riegel riegel at clearimageonline.com
Mon Aug 14 13:27:39 CDT 2006


>         // get array of all list items
>         var aItems = document.getElementsByTagName("LI");
>
>                 // bail if not found
>                 if (!aItems) return;
>
>         // for each item in the list of file nodes
>         for (iItem = 0; iItem < aItems.length; iItem++)
>         {
>                 // get array of anchors in the list item
>                 var aAnchors = aItems[iItem].getElementsByTagName 
> ("A");
>
>                         // apply behaviors to the image link
>                         if (aAnchors[0])
>                         {
>                                 aAnchors[0].onclick = jsOpenDir;
>                         }
>
>                         // apply behaviors to the text link
>                         if (aAnchors[1])
>                         {
>                                 aAnchors[1].onclick = jsSelectItem;
>                                 aAnchors[1].ondblclick = jsOpenDir;
>                         }
>         }

Hello All,

Thank you very much Paul for helping me get started in the process of  
removing my scripting from my HTML. I am starting to figure it out a  
little bit at a time. I was able to get your sample code working, it  
works perfectly. I would like to take the next step and remove  
anchors from my markup. Above is the little snip of code Paul  
provided that would read all the anchors in a LI element and apply  
behaviors to them. If I remove my anchors altogether then the markup  
changes from something like this


<li><a href="#"><img..></a><a href="#">Text Link</a></li>

to something like this...

<li><img...>Text Link</li>

The second version is much cleaner and preferable. So now for my  
question... As the above code iterates through all of the <li>  
elements how do I reference the Image and the text of the HTML? Do I  
need to use a div, or is there a way to reference without additional  
markup?

Thanks,

Terry



More information about the Javascript mailing list