Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload