Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the unique selector of elements
    primarykey
    data
    text
    <p>I am doing an editor where when user clicks a text or anything in the html document, I get it changed and record the changes in the database. It is a widget I am doing. Thus this is my question. Suppose I have an html element of the following structure:</p> <pre><code>&lt;html&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Apple&lt;/td&gt; &lt;td&gt;Pear&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Chicken&lt;/td&gt; &lt;td&gt;Beef&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div id="one"&gt; &lt;p&gt;Red&lt;/p&gt; &lt;p&gt;Orange&lt;/p&gt; &lt;/div&gt; &lt;div class="two"&gt; &lt;p&gt;Green&lt;/p&gt; &lt;p&gt;Purple&lt;/p&gt; &lt;/div&gt; &lt;div class="two"&gt; &lt;p&gt;Black&lt;/p&gt; &lt;p&gt;Blue&lt;/p&gt; &lt;/div&gt; &lt;/html&gt; </code></pre> <p>When user clicks Apple, the unique path is (Jquery):</p> <pre><code>table:eq(0)&gt;tr:eq(0)&gt;td:eq(1) </code></pre> <p>Suppose user clicks Red, the unique path is (Jquery):</p> <pre><code>div#one:eq(o) &gt; p:eq(0) </code></pre> <p>Is there any plugin readily available to uniquely identify the selectors?</p> <p>Currently I am using this function:</p> <pre><code>function getUniquePath2(node){ var parentEls = node.parentsUntil('[id]').add( node.closest('[id]') ) .map(function () { return this.tagName + (this.id ? '#' + this.id : ""); }) .get().join("&gt;"); return parentEls; } </code></pre> <p>However the above won't support and I have been researching for 4 days and I don't get an idea of how I should do it. The reason I am doing this because I want to track the user changes and change the elements as per the draft the user has made. The issue is, some elements do not have id's and the xpath conflicts with other elements. For instance if I want to change Green. How would it be unique anymore?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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