How to find out if a element belongs to a parent, javascript

Here is a function to find out if an element belongs to a particular parent.

  1. We have 2 parameters (the element id, the parent id(string))
  2. We find the direct parent of that element
  3. assign the element to the parent
  4. if the new element equals the parent string passed through, then return true
  5. If the parent is not found after walking through the DOM, then return false

function checkParent(element, parent){
while(element.parentNode){
element=element.parentNode;
if(element.id==parent){return true;}
}
return false;
}

http://www.techfind.co.uk


About this entry