function findParentNode(parentName, childObj, typefind) { var testObj = childObj.parentNode; var count = 1; while(testObj != document && testObj.getAttribute(typefind) != parentName) { testObj = testObj.parentNode; count++; } if (testObj != document && testObj.getAttribute(typefind) == parentName) { return testObj; console.log(testObj); }else{ return ""; } }