Go to last child in tree using jQuery selector
<div id="myDIV">
<div>
<span>
<a href="#">Seek me!</a>
</span>
</div>
</div>
How can I find the A tag using a jQuery selector (not looping through children ()) If I only know the id myDIV.
Well, it does sound a little awkward. For example, I clicked #myDIV and I need to get the text from the last child tag. It could be A, span, div, p, whatever. Also, myDIV cannot contain any child elements.
Sorry my english
+2
a source to share
2 answers
Like this:
$("#myDIV :not(:has(*))")
This will find all the latest "sheet" items, you can only limit <a>
, etc. if you like. For your markup:
$("#myDIV :not(:has(*))").text() // "Seek me!"
$("#myDIV :not(:has(*))").length // 1
:empty
doesn't work here (since it has a text node child), but finding things without child elements would be an example page here showing this .
+3
a source to share