puiu91
12/16/2015 - 4:47 PM

Javascript - Search parent node for id

Javascript - Search parent node for id

    /**
     * Searches a up the node tree of an element until it finds the requested id
     * 
     * @param  {obj} el     Element to start search from
     * @param  {string} id  The id to search for
     * @return {Boolean}    Whether the specified id was found while traversing up the DOM tree
     */
    function searchParentNodeForId(el, id) {
      while (el.parentNode) {
        el = el.parentNode

        if (el.id === id) {
          return true;
        }
      }
      return;
    }