Posted By

jatkins on 03/28/11


Tagged

element DOM


Versions (?)

Find an element higher in the DOM hierarchy


 / Published in: JavaScript
 

Released into the public domain. startElmnt is a reference to the lower element, e.g. document.getElementById('tdName') and endElmntTagName is just that (capitalization irrelevant, thanks to Sitepoint), e.g. TBODY.

  1. function upTo(startElmnt, endElmntTagName) {
  2. startElmntTagName = startElmnt.nodeName.toLowerCase();
  3. endElmntTagName = endElmntTagName.toLowerCase();
  4. if(startElmntTagName != endElmntTagName) {
  5. while(startElmntTagName != endElmntTagName&&startElmnt.parentNode) {
  6. startElmnt = startElmnt.parentNode;
  7. startElmntTagName = startElmnt.nodeName.toLowerCase();
  8. }
  9. }
  10. return startElmntTagName == endElmntTagName ? startElmnt : false;
  11. }

Report this snippet  

You need to login to post a comment.