closest()
selects the closest ancestor.
next()
selects only the immediate next sibling.
You want .nextAll('section').first()
.
A more semantic approach would be to wrap each group of four in a container element, then write $(this).parent().children('section')
.
Alternatively, give each section an ID, make the a
s point to that ID (<a href="#section1">
), then write $(this.id)
.
Using this approach, you can even eliminate the Javascript entirely using a section:target
CSS selector.