Sling Academy
Home/JavaScript/Building a Color Theme Picker Using the JavaScript DOM

Building a Color Theme Picker Using the JavaScript DOM

Last updated: December 12, 2024

Web development is an expansive field that bridges creativity with functionality. One of the simpler, yet visually impactful features you can implement is a color theme picker using the JavaScript DOM. This feature allows users to personalize their experience by choosing the color theme of a webpage. In this article, let’s delve into how you can build a simple color theme picker.

Understanding the Basics

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. Using JavaScript, you can manipulate the DOM to create dynamic web applications.

Setting Up the HTML Structure

To start, you'll need a basic HTML underlying structure where users can select colors. Here's a simple setup:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Theme Picker</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Choose Your Color Theme</h1>
    <select id="themePicker">
        <option value="" selected disabled>Select Theme</option>
        <option value="light">Light</option>
        <option value="dark">Dark</option>
        <option value="blue">Blue</option>
        <option value="green">Green</option>
    </select>
    <script src="script.js"></script>
</body>
</html>

Styling Your Web Page

Next, create a CSS file, for instance styles.css, to define the themes. Here's an example of how you might set up simple themes:

body {
    transition: background-color 0.5s ease-in-out;
    margin: 0;
    font-family: Arial, sans-serif;
}

body.light {
    background-color: white;
    color: black;
}

body.dark {
    background-color: black;
    color: white;
}

body.blue {
    background-color: #a8dadc;
    color: #1d3557;
}

body.green {
    background-color: #a8ddb5;
    color: #07342f;
}

JavaScript to Change Themes

Now, let’s add the interactive part using JavaScript. You'll need to write code that listens for a change in the selector and applies the appropriate theme class to the <body> element. Create a script.js file:

document.addEventListener('DOMContentLoaded', function () {
    const themePicker = document.getElementById('themePicker');

    themePicker.addEventListener('change', function() {
        const selectedTheme = themePicker.value;
        document.body.className = '';
        document.body.classList.add(selectedTheme);
    });
});

Connecting All the Pieces

Once you have your HTML, CSS, and JavaScript set up, test your setup by opening your HTML file in a browser. You should be able to select different themes from the dropdown menu and see the changes reflect on the page style.

Reusable and Extensible

This basic implementation of a color theme picker is both reusable and extensible. You can easily add more color themes by modifying your CSS and expanding the options in your HTML <select> element. Similarly, you can include additional features like saving the selected theme in the local storage for a persistent user experience:

themePicker.addEventListener('change', function() {
    const selectedTheme = themePicker.value;
    document.body.className = '';
    document.body.classList.add(selectedTheme);
    localStorage.setItem('theme', selectedTheme);
});

// Apply theme on page load
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
    document.body.classList.add(savedTheme);
}

Conclusion

Using the JavaScript DOM to manipulate webpage themes is an excellent exercise in enhancing dynamic experiences in web applications. The color theme picker is a simple, yet powerful feature that lets users personalize their browsing experience, which can boost user satisfaction and engagement.

Next Article: Async and the DOM: Waiting for Data Before Rendering in JavaScript

Previous Article: Optimizing Image Galleries with Lazy Loading in JavaScript

Series: JavaScript: Document Object Model Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration