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?
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?
closest()
, but of course that doesn't tell me just how close it is, so I can't tell it to choose the closest "h", using a switch/if...else if.
– Bluefire
Commented
Sep 21, 2012 at 18:01
The ma 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.