Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Just a cross-browser interpretation, only returns true or false, though.

Description: Function that returns true when a specific node lies within the node tree of another.

Syntax: contains(in Node, reference Node)

function contains(a, b) {
      for(var i = a.childNodes, x = 0; x < i.length; x ++)
            if(i[x] == b || (i[x].hasChildNodes() && contains(i[x], b)))
                  return true;
      return false;
}


Example:

<html>
      <head></head>
      <body>
            <div><span id="der"></span></div>
            <script type="text/javascript">
            <!--
            var i = document.getElementById("der");
            if(contains(document.getElementsByTagName("div")[0], i))
                  i.appendChild(document.createTextNode("Text"));
            //-->
            </script>
      </body>
</html>


newBookmarkLockedFalling