Posted By

xzkcz on 01/12/09


Tagged

XPath


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

oamaxa


getElementXPath


 / Published in: JavaScript
 

URL: http://snippets.dzone.com/search/get_results?q=getElementXPath

  1. function getElementXPath(elt)
  2. {
  3. var path = "";
  4. for (; elt && elt.nodeType == 1; elt = elt.parentNode){
  5. idx = getElementIdx(elt);
  6. xname = elt.tagName;
  7. if (idx > 1) xname += "[" + idx + "]";
  8. path = "/" + xname + path;
  9. }
  10. return path;
  11. }
  12.  
  13. function getElementIdx(elt){
  14. var count = 1;
  15. for (var sib = elt.previousSibling; sib ; sib = sib.previousSibling){
  16. if(sib.nodeType == 1 && sib.tagName == elt.tagName){
  17. count++
  18. }
  19. return count;
  20. }

Report this snippet  

You need to login to post a comment.