user293678
user293678

Reputation:

Selecting Parent's Child in jQuery

I think all I need is a selector, but can't find one. I want to select the anchor tag, but can only select the li using .parent()...

jQuery:

$("nav ul li > ul").parent().append('<span class="arrow">&darr;</span>');

HTML:

<li><a href="#">The Events</a>
  <ul>
    <li><a href="#">Schedule</a></li>
    <li><a href="#">Buy Tickets</a></li>
    <li><a href="#">Features</a></li>
  </ul>
</li>

The code above puts the arrow on the right lis, but one level to high. I need to target the li's a tag.

Upvotes: 1

Views: 2454

Answers (3)

Nick Craver
Nick Craver

Reputation: 630637

You can use .siblings(), like this:

$("nav ul li > ul").siblings("a").append('<span class="arrow">&darr;</span>');

You can find a full list of traversal functions here.

Upvotes: 4

Timbadu
Timbadu

Reputation: 1551

Does this happen on clicking the link or onload?

Upvotes: 0

user293678
user293678

Reputation:

Answered myself. Using .children("a") I was able to select the anchor. Final code looked like:

$("nav ul li > ul").parent().children("a").append('<span class="arrow">&darr;</span>');

Upvotes: 0

Related Questions