tags:

views:

51

answers:

3
<div id="myDIV">
 <div>
  <span>
    <a href="#">Seek me!</a>
  </span>
 </div>
</div>

How can I find A tag using jQuery selector (not looping through children()) If I know only myDIV id.

Well, it really sounds a bit awkward. For example I've clicked #myDIV and i need to get text from the last child tag. It could A, span, div, p, whatever. Also myDIV could not even have any children elements.

Excuse me my English

A: 

It's just $('#myLink')

Zlatev
A: 

Try:

$("#myLink")
Oraclee
+1  A: 

Like this:

$("#myDIV :not(:has(*))")

This will find all the last "leaf" elements, you could restrict to only <a>, etc if you wanted. For your markup:

$("#myDIV :not(:has(*))").text() // "Seek me!"
$("#myDIV :not(:has(*))").length // 1

:empty doesn't work here (because it has a text node child), but finding things with no child elements will, here's an example page showing this.

Nick Craver
@downvoter - Enlighten us on a better method? Seeing as every other answer here doesn't work I find it interesting you down-vote the only answer that does.
Nick Craver
@Nick: I'll even it out for you :-) I had to re-read the question a few times, though.
Andy E
how can i adjust your code if i use $(this) to define the clicked element? thanks
dr3w
@dr3w - Which element are you clicking?, the `#myDIV`?
Nick Craver
yes, #myDIV. But what if it didn't have any id?
dr3w
@dr3w - Whatever `this` is, you can do `$(this).find(":not(:has(*))")` if you need to sometimes include even the element clicked, then: `$(this).find(":not(:has(*))").andSelf().filter(":not(:has(*))")`
Nick Craver