user198729
user198729

Reputation: 63686

How to get first parent of span that is a <p> in jQuery?

As per this page says:

Find all parent elements of each span that is a paragraph.

How to get the first one that matches?

Upvotes: 3

Views: 6668

Answers (4)

TheVillageIdiot
TheVillageIdiot

Reputation: 40512

$("span").parents("p :first")

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 828090

If you mean by first parent the one that is closest to the element, you can use Traversing/closest:

$(selector).closest('p');

Upvotes: 2

theraneman
theraneman

Reputation: 1630

jQuery returns such results in an array. Have you tried using [0] after the query. $("span").parents("p")[0]. This would give the first parent.

Upvotes: 1

Brandon Henry
Brandon Henry

Reputation: 3719

$("span").parents("p:first")

Upvotes: 6

Related Questions