You're doing some really weird stuff there.
Your last line with page[i+1]
will actually never work since page
is not an array. I suppose you probably meant pages[i+1]
.
If you actually meant what I just said, then it's common sense that it will return block
. Just consider the case where you have two .form-page
elements. When clicking your button the following happens:
First iteration:
Your first element with ID #form-part1
has display: block
because you set it that way initially. This obviously means the if()
statement inside the forEach()
is being executed since it's not equal to "none"
.
This means that it sets the style of #form-part1
to display: none
, but it also means it sets the next element CSS to display: block
.
Second iteration:
Your second .form-page
now has display: block
(read the last sentence of the previous iteration). This means the if()
is also executed, setting the style back to display: none
.
It will also try to find the next page, but there are no other pages anymore so it will also throw an error (another fault in your code).
So you obviously have to recheck your code since you're doing some thing(s) wrong. You will also have to rephrase your question because right now it's unclear what you're actually trying to achieve. But to me it makes sense that if you're having 2 pages, the if()
statement is always being executed.