onClick event doesn't work on initial homepage load

Wix and Velo newbie here. I have created an onClick event to expand/collapse a container box. The way I have it set up is the container box is collapsed. Pressing button 1 (which is in the header) causes the container to expand, and pressing button 2 collapses the container again. The two buttons are stacked on top of each other (in the header) so it functions as “one button” for the user.

The problem occurs on first load of my site (homepage). Pressing on the button does not expand the container box. However, if you go to another page and then try to click button 1 (my header is on each page), the container expands and collapses as it should be. And if you go back to the original homepage, the button will now work as it should.

Does anyone have an idea as to why one needs to go to a different page before this onClick event works properly? Here’s my code below:

export function oh1_click ( event ) {
$w ( ‘#officehoursbox’ ). expand ();
$w ( ‘#oh1’ ). collapse ()
$w ( ‘#oh2’ ). expand ()
}
$w ( ‘#oh2’ ). onClick (()=>{
$w ( ‘#officehoursbox’ ). collapse ()
$w ( ‘#oh2’ ). collapse ()
$w ( ‘#oh1’ ). expand ()

})

edit: I just tried on a different browser and started on a different page than the home page. The onClick event did NOT work on that initial page load, but going to another page triggered it to work properly. But then I clicked on the home page and it didn’t work. Clicking to another page and then BACK to the home page made the onClick event to work. Something weird is going on. Maybe it is the order of the buttons and/or how they’re stacked on top of each other. Any suggestions are appreciated.