Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i need the full dom node path of element

Tags:

dom

i need to search the html document for <p class="content"> text here </p>

and then output the full node path (CSS or XPATH)

for isntance

html > body > div class ="something" > table > tr > p class="content"

i tried with nokogiri but it doesn't handle the class and other attributes well..

i need a parser that does this without problem.

like image 992
h34y Avatar asked Sep 28 '09 00:09

h34y


1 Answers

Ok try this:

var path = [];

var el = document.getElementById('myNode');

do {
    path.unshift(el.nodeName + (el.className ? ' class="' + el.className + '"' : ''));
} while ((el.nodeName.toLowerCase() != 'html') && (el = el.parentNode));

alert(path.join(" > "));
like image 65
nickf Avatar answered Oct 26 '22 09:10

nickf