0

I'm using the script found here to get some text that defines the path to an HTML element. The results look something like this:

html>body>section:eq(3)>ul>li:eq(1)>div

How do I go about using either JavaScript or jQuery to select the element using these results?

Community
  • 1
  • 1
opticon
  • 3,494
  • 5
  • 37
  • 61

1 Answers1

0

Assuming that you save that string is stored in the variable "path", with jQuery you can do

var node = $(path);

and retrieve the exact element(s). The last "div" may have siblings of the same tag type and can retrieve an array instead of an element.

As for a JS implementation, it's a little bit hard. You'd have to reimplement sizzle or you can include it in your project and call:

var node = Sizzle(path);

jQuery node selection is based on it (when using complex selectors. it's a little bit smart and uses pure JS when detects ids, classes or plain tags).

Mihail Alexe
  • 340
  • 2
  • 9