Select the closest element of type "h", for example. h1, h2 etc.

Suppose I select an item using $(mySelector) . I would like to select the closest title to it, so if the closest title element to it was <h2> , he would select it, but if the nearest was <h3> , he would choose it instead. How can i do this?

+4
source share
1 answer

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

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

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

+8
source

Source: https://habr.com/ru/post/1435554/


All Articles