I've just written a little javascript function that I have myself found extremely valuable when doing DOM scripting. It's called parentElementsByTagName()
and even if it's name isn't great it really does work and has proven very useful for my app.
At any starting point in a DOM tree (first parameter) it goes "up the tree" by looping over "currentelement.parentNode". The second parameter is the tag name (eg. "div"
) and the third parameter is optional and it's a class name that that tag name needs to have.
I wrote this because I found myself writing this.parentNode.parentNode.parentNode...
in my Javscript code and thought I'd stop that sillyness.