about photos bookshelf portfolio blog home
Begin main content

Simple javascript DOM visitor

I had a need to alter a bunch of ids and names after I cloned a dom node, so I cooked up the following dom node visitor:
function domVisitor(el, f) {
  if (el) {
    f(el);
    for (var i=0 ; i < el.childNodes.length ; i++) {
      domVisitor(el.childNodes[i], f);
    }
  }
}
So all you need to do is pass in the cloned node (or whatever) along with an anonymous function to do your bidding. The function will be called for the full tree under the node passed in.

06:38 PM, 05 Jul 2007 by Mark Aufflick Permalink

Add comment