Question

As per this page says:

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

How to get the first one that matches?

Was it helpful?

Solution

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

OTHER TIPS

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

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top