Aha! Excellent thank you :) Sent from my iPhone
On 9 May 2011, at 13:38, Nick Morgan <[email protected]> wrote: > You need to `return` the recursive call: > > } else { > > return findParentElementWhichIs(parent, elem); > > } > > That way it gets sent back up the call stack when the result is found. > > Nick > > On 9 May 2011 13:31, Mark McDonnell <[email protected]> wrote: >> Hi, >> I have the following code... >> >> function findParentElementWhichIs(startingPoint, elem) { >> >> var parent = startingPoint.parentNode, >> >> parentname = parent.nodeName.toLowerCase(); >> >> >> >> if (parentname === 'body') { >> >> return false; >> >> } >> >> >> >> if (parentname === elem) { >> >> return parent; >> >> } else { >> >> findParentElementWhichIs(parent, elem); >> >> } >> >> } >> >> ...if the parent element is found straight away then the function will >> return the parent node but if the function can't find the element and has to >> recursively call itself then the function refuses to return anything other >> than undefined? >> See this jsFiddle for an example: http://jsfiddle.net/WkeYX/ if you change >> the code to look for the next element up (which is an <li>) then it works >> fine and returns the element, but as it stands it correctly exits from the >> function once it finds the <div> element but just doesn't return it. >> Any ideas? >> Kind regards, >> Mark >> >> -- >> To view archived discussions from the original JSMentors Mailman list: >> http://www.mail-archive.com/[email protected]/ >> >> To search via a non-Google archive, visit here: >> http://www.mail-archive.com/[email protected]/ >> >> To unsubscribe from this group, send email to >> [email protected] >> > > > > -- > Nick Morgan > http://skilldrick.co.uk > @skilldrick > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
