Error: Cannot read properties of undefined (reading ‘remove’) When using .classList.remove [duplicate]

Twine Version: 2.3.16
Story Format: Sugarcube 2.36.1

So I’m trying to make a gallery that the player can scroll through. I’ve already set up the pictures and everything, but right now, I’m trying to make it so when the page is turned, the pictures on the first page disappear, and the second page appears. To that end, I made it so when the variable for the page number, gPN = 1, the second page of pictures has the class Hidden, which sets their opacity to 0. Alternatively, when gPN = 2, the Hidden class is removed, and added to the images from the first page. Here is the code.

if (!window.PC){
  window.PC = {};
}
PC.PageSwitch = function (dir) {
        var image1 = document.getElementsByClassName("galimg1")
        var image2 = document.getElementsByClassName("galimg2")
        if (dir === 1) {
                variables().gPN - 1
        }
        else if (dir === 2) {
                variables().gPN + 1
        }
        if (variables().gPN <= 0) {
                variables().gPN = 1
        }
        //Page Class Modifiers
        if (variables().gPN === 2) {
                image1.classList.add('Hidden');
                image2.classList.remove('Hidden');
        }
        else if (variables().gPN === 1) {
                image1.classList.remove('Hidden');
                image2.classList.add('Hidden');
        }
};

My issue is that when I press the next button, I get the following error.

Error: Cannot read properties of undefined (reading 'remove')

I checked to make sure everything was kosher, in case something had mismatching capitalization, but everything is matching. I don’t know how to fix this.