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:
<html>
<table>
<tr>
<td>Apple</td>
<td>Pear</td>
</tr>
<tr>
<td>Chicken</td>
<td>Beef</td>
</tr>
</table>
<div id="one">
<p>Red</p>
<p>Orange</p>
</div>
<div class="two">
<p>Green</p>
<p>Purple</p>
</div>
<div class="two">
<p>Black</p>
<p>Blue</p>
</div>
</html>
When user clicks Apple, the unique path is (Jquery):
table:eq(0)>tr:eq(0)>td:eq(1)
Suppose user clicks Red, the unique path is (Jquery):
div#one:eq(o) > p:eq(0)
Is there any plugin readily available to uniquely identify the selectors?
Currently I am using this function:
function getUniquePath2(node){
var parentEls = node.parentsUntil('[id]').add( node.closest('[id]') )
.map(function () {
return this.tagName + (this.id ? '#' + this.id : "");
})
.get().join(">");
return parentEls;
}
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?