Bluefire
Bluefire

Reputation: 14129

Select closest element of type "h", e.g. h1, h2, etc

Assume I select an element using $(mySelector). I would like to select the closest heading to it, so if the closest heading element to it was an <h2>, it would select that, but if the closest was an <h3>, it would select that instead. How can I do this?

Upvotes: 4

Views: 3014

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382314

The comma in selectors means "or". So you may do this :

$(mySelector).closest('h3, h2')

This will return 0 or 1 element, the closest if more than one match.

Upvotes: 8

Related Questions