<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: paternity code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 22:04:19 GMT</pubDate>
    <description>DZone Snippets: paternity code</description>
    <item>
      <title>Object paternity</title>
      <link>http://snippets.dzone.com/posts/show/1859</link>
      <description>// object paternity in Javascript&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// relationship of object 1 to object 2&lt;br /&gt;function objectPaternity(obj1, obj2) {&lt;br /&gt; // given an object this function returns 'child' if obj1 is a child of obj2&lt;br /&gt; // and 'parent' if obj1 is a parent of obj2 - then 'no' if neither&lt;br /&gt; var relationship = 'no';&lt;br /&gt; var currentElement = obj1;&lt;br /&gt; &lt;br /&gt; while((typeof currentElement.parentNode != 'undefined') &amp;&amp; (currentElement != document.body)) {&lt;br /&gt;  // first cycle through all the parent elements of object 1 to see if object 2 is one of them&lt;br /&gt;  if(currentElement.parentNode == obj2) {&lt;br /&gt;   relationship = 'child';&lt;br /&gt;  }&lt;br /&gt;  // walk up the tree and try again&lt;br /&gt;  currentElement = currentElement.parentNode;&lt;br /&gt; }&lt;br /&gt; // if object 1 is not a child of object 2 then we test the other way arround&lt;br /&gt; if(relationship == 'no') {&lt;br /&gt;  currentElement = obj2;&lt;br /&gt;  while((typeof currentElement.parentNode != 'undefined') &amp;&amp; (currentElement != document.body)) {&lt;br /&gt;   // now cycle through all the parent elements of object 2 to see if object 1 is one of them&lt;br /&gt;   if(currentElement.parentNode == obj1) {&lt;br /&gt;    relationship = 'parent';&lt;br /&gt;   }&lt;br /&gt;   // walk up the tree and try again&lt;br /&gt;   currentElement = currentElement.parentNode;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; return relationship;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 07 Apr 2006 17:07:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1859</guid>
      <author>nataliedowne (Natalie Downe)</author>
    </item>
  </channel>
</rss>
