Hello,
I'm trying to do a function that when I click on an element I can get either the tree (all parents) of the element (up to the body or a specific tag), or get something similar to "Copy -> Copy Selector "from DevTools.
ForexamplewhenIclickonb
Ireturnsomethinglikediv>nav>h3>b
.
Byclickingonlabel
returndiv>label
.
Byclickingonh3
returndiv>nav>h3
.
Youcanincludethebodyifyouwant...Ifyouneedtoinsertanidintoanelementtoknowwheretostopisaviablesolutionaswell.
Thankyouinadvance!
Edit
Improvingthedoubt...
Let'ssupposeIhaveanHTMLstructureinsideadiv.
Asforexample:
<divid="stack-ex">
<div>
<nav>
<label>Teste</label>
</nav>
<label>Texto 2</label>
</div>
</div>
I need to list the content of #stack-ex
something like this:
- div
- nav
- label
- label
- nav
So the structure of <li>
is a reference to the contents of #stack-ex
, I would later need along with the list to have the selector
of the element that the list item references. Then I'll put something like a button to change color, or other cases.
So by clicking, you would need to get the parent
of it because then you would only use $(aqui)
being aqui = #stack-ex > div > nav > label
.
The click was just something to exemplify for you to help me with a practical example, I will not necessarily click and call the function.