Website design dark grey on darker grey10/12/2023 But a server-side solution like this is useful in persisting the user’s theme choice across page reloads, as we will see later. This method has an obvious downside: the page needs to be refreshed for the toggle to take place. I am using a GET request (URL params) for the purpose of this demonstration.Īnd, yes, we can swap stylesheets just like we did in the second method. Then, we let our code (PHP in this case) apply the appropriate body class when the page is reloaded. We can have the user send a GET or POST request. This is a great approach if you prefer working directly in the markup. If we’re already working with a server-side language, say PHP, then we can use it instead of JavaScript. Here’s a script for a button that will toggle that class, for example: // Select the buttonĬonst btn = document.querySelector('.btn-toggle') ītn.addEventListener('click', function(). The trick here is to swap out a class that can be a hook for changing a style anywhere on the page.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |