Reputation: 818
I have a button and I'd like, on click, for it to scrollIntoView()
to a header tag that comes just before the button (basically 'scroll to top of this section', but without fixed id's), but I'm not sure how to format the selector in order to reference that header. I am working in the onClick
callback with the button as event.currentTarget
.
The button
is inside of a nav
and is thus not directly following the header, it's roughly as follows:
<h5/>
<nav>
<button/>
</nav>
<this sections content/>
There are several of this general pattern in a row, without any sort of seperating containers, so first-of-type
won't work.
What else do I need to add to my current $(e.currentTarget)
to select that <h5>
Upvotes: 0
Views: 215
Reputation: 782325
$(this).closest("nav").prev("h5")
.closest("nav")
goes up to the containing <nav>
element.
.prev("h5")
returns the preceding element, as long as it's <h5>
.
Upvotes: 2