I'm using a simple DOM parser function to catch XPath information of all nodes of a document
For example, the HTML is:
<div>
<div>Everyday People</div>
<div>My name is ACE</div>
<div>Hello world</div>
</div>
Parsing the DOM to store the XPath info in the array arr
:
<script type="text/javascript" src="js/jquery/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery/js/xpath-selector.js"></script>
<script type="text/javascript">
function get_XPath(elt)
{
var path = '';
for (; elt && elt.nodeType == 1; elt = elt.parentNode)
{
var idx = $(elt.parentNode).children(elt.tagName).index(elt) + 1;
idx > 1 ? (idx='[' + idx + ']') : (idx='');
path = '/' + elt.tagName.toLowerCase() + idx + path;
}
return path;
}
var arr = Array();
htmlDoc = document;
x = htmlDoc.documentElement.childNodes;
for (i=0; i<x.length; i++)
{
arr.push(get_XPath(x[i]));
}
</script>
Later on in the script I use the values stored in arr
to perform some functions like showing, hiding or changing content of nodes.
<script>
for(i=0;i<arr.length;i++)
{
//catch the object reference with the XPath info
$(arr[i])
}
</script>
In the snippet above, I'm getting an object but I'm unable to get the object reference to use it for something like:
$(arr[i]).text();
Any help would be greatly welcome. Anyone worked on jQuery XPath selectors?