Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 206
Bluefire Avatar asked Feb 20 '23 02:02

Bluefire


1 Answers

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.

like image 146
Denys Séguret Avatar answered Feb 21 '23 17:02

Denys Séguret