Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get XPath of a DOM element with jQuery

I need to get the XPath of a DOM element to persist it so I can look for that element lather.

I've tried the getPathTo method of this answer but when I call the method with a jQuery-created object like this...

getPathTo(jQuery('h3').first());

...I get this error:

Uncaught TypeError: Cannot read property 'childNodes' of undefined(…)

I've tried to replace parentNode with parent(), childNodes with children(), and tagName with prop('tagName'), but then I received undefined as the function result...

So, do you have a similar function to getPathTo that works with jQuery?

like image 591
fsinisi90 Avatar asked Apr 06 '16 13:04

fsinisi90


1 Answers

The method expects a DOM node and you are giving it a jQuery object

getPathTo(jQuery('h3').first()[0])

or

getPathTo(jQuery('h3').first().get(0))
like image 185
epascarello Avatar answered Oct 28 '22 08:10

epascarello