prestomanifesto
prestomanifesto

Reputation: 12796

Descendant of sibling jquery

I have recently switched from using XPath to using jQuery selectors. I am having trouble with a particular XPath selector I would use:

//h1[contains(.,'Some Title')]//following::a[1]

Basically I get the first link element from a descendant of a sibling.

From what I understand jQuery selects descendants as "ancestor descedant" and selects siblings as "sibling + sibling".

How do I combine these when I don't care/know what the sibling tag is? (In other words "h1 + ul a" works but I want to leave out the ul if possible"

Upvotes: 1

Views: 1202

Answers (2)

avetarman
avetarman

Reputation: 1252

If you have only one sibling for your H1, you can use

$('h1').siblings().find('a')

If you want to use exactly the NEXT sibling, do

$('h1').next().find('a')

Upvotes: 2

Scott Ferguson
Scott Ferguson

Reputation: 7830

Do you mean like jQuery('ancestor descendant') ?

Check out http://api.jquery.com/descendant-selector/

For example:

$("h1 a")

Upvotes: 0

Related Questions