[thelist] DOM: previousSibling and LI tags

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Fri Jan 20 15:45:48 CST 2006

> sweep = function(obj) {
>   if (!obj) obj=document;
>   var children=obj.childNodes;
>   for (var j = children.length-1; j >= 0; j--) {
>   // if node is a whitespace-only text node
>   if (children[j].nodeType==3 && /^\s*$/.test(children[j].nodeValue))
>   obj.removeChild(children[j]);
>   else if (children[j].hasChildNodes())
>   sweep(children[j]);
>   }
> }

A yes, you're right. Traversing the nodes in reverse order is better.

Another option would be caching the node references to an array:

pseudocode (because I'm lazy):

arCache = new Array

for each child node in obj
   arCache.push childnode

for i=0 to arCache.length
   if the ith node is whitespace remove it.
   else if it has children call this method recursively.


Although it adds an extra for loop, it guarantees to be independent of
the "live"liness of the
DOM nodes.

I think PPK should open a "most complicated, mind-crushing,
brain-teasing  nextSibling wrapper implementation" contest :))

Volkan Ozcelik
+>Yep! I'm blogging! : http://www.volkanozcelik.com/volkanozcelik/blog/
+> My projects/studies/trials/errors : http://www.sarmal.com/

More information about the thelist mailing list