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.

04:38 AM, 05 Jul 2007 by Mark Aufflick Permalink | Comments (0)

XML

Blog Categories

software (41)
..cocoa (23)
  ..heads up 'tunes (5)
..ruby (6)
..lisp (4)
..perl (4)
..openacs (1)
mac (21)
embedded (2)
..microprocessor (2)
  ..avr (1)
electronics (3)
design (1)
photography (26)
..black and white (6)
..A day in Sydney (18)
..The Daily Shoot (6)
food (2)
Book Review (2)

Notifications

Icon of envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. Unregistered Visitor: emanations
  2. Unregistered Visitor: MAHI
  3. Unregistered Visitor: beside
  4. Unregistered Visitor: dsf
  5. Unregistered Visitor: kentucky durby
  6. Unregistered Visitor: kentucky durby
  7. Unregistered Visitor: kentucky durby
  8. Unregistered Visitor: kentucky durby
  9. Unregistered Visitor: kentucky durby
  10. Unregistered Visitor: kentucky durby