Next/previous section button

Hi! I’m implementing a next/previous section button or anchor on my website using custom code. Here’s the code for the next button I’ve tested using VS Code. I’ve tested it and it works as expected.

<body>
    <section class="section" id="0"></section>
    <section class="section" id="1"></section>
    <section class="section" id="2"></section>
    <section class="section" id="3"></section>
    <section class="section" id="4"></section>
    <section class="section" id="5"></section>
    <a id="nextButton" href="#0">Next Button</a>
</body>

<script>
    let currentSection = 0;
    let nextButton = document.getElementById("nextButton");

    window.addEventListener('scroll', () => {
        const newSection = Math.round(window.scrollY / window.innerHeight)
        if (newSection != currentSection) {
            nextButton.href = "#" + (newSection + 1);
            currentSection = newSection;
        }
    })
</script>

I tried pasting the script using embed but unfortunately it doesn’t work. What am I missing here?

Nevermind, I’ve got the solution… and it’s silly - it should be document.addEventListener not window.addEventListener. Spent hours trying to find the solution…

<script>

let thisSection = 0;
let nextButton = document.getElementById("anchor-next-button");

document.addEventListener('scroll', () =>
{
    const newSection = Math.round(window.scrollY / window.innerHeight)
    if(newSection != thisSection)
    {
        nextButton.href = "#"+(newSection+1);
        thisSection = newSection;
    }
})

</script>