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?
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?
- Why not just store a reference to that element in a variable within scope of all the required functions? – Rory McCrossan Commented Apr 6, 2016 at 13:20
-
getPathTo(jQuery('h3').first()[0])
– epascarello Commented Apr 6, 2016 at 13:21 - I Think your Problem is that you use a documentElement in jQuery Context. You can try to extract the element to javascript native level. Like this: var element = jQuery('h3'); element.childNodes.... – Denis Kohl Commented Apr 6, 2016 at 13:27
- @RoryMcCrossan because I need the element to be retrieved in another session. – fsinisi90 Commented Apr 6, 2016 at 13:27
- @epascarello Thanks, it works. Maybe you want to post it like an answer. – fsinisi90 Commented Apr 6, 2016 at 13:27
1 Answer
Reset to default 5The 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))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742370037a4431080.html
评论列表(0条)