Skip to content

Instantly share code, notes, and snippets.

@KevinAlavik
Last active August 6, 2023 18:23
Show Gist options
  • Save KevinAlavik/b9e7c470d9c03fd4e6628e1c525a5651 to your computer and use it in GitHub Desktop.
Save KevinAlavik/b9e7c470d9c03fd4e6628e1c525a5651 to your computer and use it in GitHub Desktop.
Simple page switching function
let displayType = 'flex';
function switchPage(page) {
const pages = document.querySelectorAll('.page-container');
for (let i = 0; i < pages.length; i++) {
if (pages[i].getAttribute('page') === page) {
pages[i].style.display = displayType;
} else {
pages[i].style.display = 'none';
}
}
}
@KevinAlavik
Copy link
Author

KevinAlavik commented Jun 3, 2023

Example:

 <a onclick="switchPage('user')">Go To User Page</a>

<div class="page-container" id="homePage" page="home" style="display: flex;">
    <h1>Home Page</h1>
</div>

<div class="page-container" id="userPage" page="user" style="display: none;">
    <h1>User Page</h1>
</div>

<!-— Home Page will be visible on the start of the page but not userPage but if the user clicks Go To User Page the homePage will be invisible —-> 

@KevinAlavik
Copy link
Author

KevinAlavik commented Jun 3, 2023

To use any other displaytype other the flex just change the variable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment