If/then Logic in dynamic pages

I am using following code in static page, which is working fine. But how can I solve the issue to avoid duplication of code in dynamic pages.

if( $w(“#myElement”).value === “1” )
{ $w(“#myElement2”).show(); }
else
{ $w(“#myElement2”).show(); }

This code is about user input, I am not using wix forms.

If I use this code in dynamic pages, this will be applied to all pages.
What can be done to solve this issue. I have to configure value in the input box.

Hello.

If you are using page index logic in your code, you can use the currentPageIndex defined here to differentiate pages and perform you calculations.

Good luck!

Thank you for the response. I will look into it.