3

Is there a way, using jquery to get an XPath query of the current text selection?

Thanks for your help,

shanabus
  • 12,989
  • 6
  • 52
  • 78
AP.
  • 5,205
  • 7
  • 50
  • 94
  • possible duplicate of [How to calculate the XPath position of an element using Javascript?](http://stackoverflow.com/questions/3454526/how-to-calculate-the-xpath-position-of-an-element-using-javascript) – Fabian Schmengler Feb 13 '13 at 14:18

2 Answers2

0

try this:

Updated It should work now:

var path = "/" + element.parents().map(function () {
    return this.tagName;
}).get().reverse().join("/");

path += "/" + element.prop("tagName") + "[" + (element.prevAll().length + 1) + "]";

http://jsfiddle.net/95PnT/5/

alijsh
  • 842
  • 7
  • 7
  • 1
    This is not good enough. If there are multiple siblings at a level with the same tag name, you don't know which one to choose. – Felix Kling Feb 13 '13 at 13:49
  • it gets the parents of an element at first step: $(element).parents() – alijsh Feb 13 '13 at 13:51
  • 2
    @alijsh: Yes, so it generators something like `a/b/c`. What if `a` has two `b` children? How do you know which one to select when you are trying to apply the XPath? You have to consider this information as well. – Felix Kling Feb 13 '13 at 13:56
0

No, there is no built-in method to do this using jQuery. There are, however, other javascript options, see here: How to calculate the XPath position of an element using Javascript?

Community
  • 1
  • 1
Ruslan
  • 9,927
  • 15
  • 55
  • 89