You're just missing a return
statement in your else
clause. It should be:
...
} else {
return getC( P.parentNode );
}
Note that you're using a recursive function (a function that calls itself), so you should probably add extra precautions to make it return something in exceptional cases (such as, there is no node with class "root"), otherwise you'll get an infinite recursion, and a stack overflow error.