Recursion

One problem I have encountered is that sometimes templates differ, and one person may have paragraph tags inside a form, with input elements inside them. Other people may have another setup, such as all input elements inside the one paragraph tag, except the comment box. Sometimes, I needed to be able to find the form element that was the parent of a particular element, and sometimes this was it’s immediate parentNode, sometimes it was the grandparent node (node.parentNode.parentNode). So I wrote this nice little function that uses recursion to find the parent that is a form. I’m fairly sure it works okay, and there’s even a little check that will return and explicit false if there is no parent form.

1     function getParentForm(node){
2         if (node==document) 
3             return false;
4         if (node.tagName!="FORM") 
5             return getParentForm(node.parentNode); 
6         else 
7             return node;
8     }
blog comments powered by Disqus