Revamp the hues of numerous elements simultaneously using just a single click!

Is there a way to change the colors of various elements (footer, divs, text) background and text color at once using just one button? I attempted to use JavaScript buttons, but I'd prefer not having to name each individual object's button in order to alter their colors. Appreciate any guidance. Thank you.

Answer №1

Here is a simple way to change the color of elements on your webpage:

<button id="btnChangeColor" class="btn btn-primary">Change Color</button>

Use JavaScript to add an event listener to the button and select all elements you want to change with the DOM.

const btnChangeColor =    document.querySelector('#btnChangeColor');
const footer =            document.querySelector('footer');
const divs =              document.querySelectorAll('div');

btnChangeColor.addEventListener('click', () => {
    footer.classList.add('custom-theme');
    divs.forEach(item => {
        item.classList.add('custom-theme');
    });
});

You can customize the CSS class (custom-theme) with your preferred values:

.custom-theme {
    background-color: green;
    color: white;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a 3x3 grid with CSS grid is great, especially when you want to place items in specific columns within the grid

I'm attempting to design a layout that has a unique structure: https://i.sstatic.net/dCLoPm.png The blue rectangles are represented by divs. It's like a 3x3 grid, but with spaces in the 3rd and 4th columns of the grid. How can I incorporate th ...

What could be the reason behind the failure of this computed property to update in my Vue 3 application?

As I transition from Vue's Options API to the Composition API, I decided to create a small Todo App for practice. Within App.vue, my code looks like this: <template> <div id="app"> <ErrorMessage v-if="!isVali ...

Retrieving JSON data from outside the React root directory

My current project includes an older javascript/php application with numerous JSON files used to retrieve data from the database. As I plan to migrate some modules to React, I am wondering if it's possible to still fetch data from these JSON files wi ...

PNG distortion caused by pngjs in a Node.js environment

As I attempt to slice PNG spritesheets into sprites using pngjs v3.3.0 for nodejs, I encounter an unexpected issue of noisy backgrounds in some of the produced sprites. To investigate, I created a simple script that generates an empty transparent PNG and s ...

Developing shell scripts and utilizing jQuery

I want to execute a linux command that returns the currently logged on user in linux. Using jQuery and ajax, passing this command to the PHP, which will receive and execute this command using exec() function. The value should be stored and returned. Upon ...

Add the base target HTML to the Google Maps iframe using jQuery

On my client's website, I integrated Google Maps using an iframe. The issue is that when users click on external links within the map descriptions, the linked websites load inside the iframe itself. I am trying to find a solution to make these externa ...

Elevate the opacity with a hover effect

I am currently in the process of building my own website using siteorigin page builder on wordpress. One issue I have encountered is that they do not offer a hover option, so I had to create a custom CSS to add a hover effect on the background color. When ...

I have difficulty generating appropriate pseudonyms

Struggling to create aliases in my react project (CRA 3.1.1) has been a challenge for me. Despite attempting various methods, I have not been successful in achieving it. The only success I've had so far is aliasing the simple "src" folder based on som ...

Inserting HTML code into the polymer

This is my HTML code: <div id="setImgWrap"> <!-- The appended image will be displayed here --> </div> Here I am appending an image from JavaScript: this.addedImages = { imageURL:self.downloadURL }; this.$.setImgWrap.append('& ...

Endless callback loop in CoffeeScript combined with JQuery

We are currently facing an issue with the CoffeeScript code provided below. The get_job_status function is responsible for retrieving the status of a task from the Python backend using AJAX. It continues to check the task status regularly until it changes ...

What is the best way to test for errors thrown by async functions using chai or chai-as-promised?

Below is the function in question: async foo() : Promise<Object> { if(...) throw new Error } I'm wondering how I should go about testing whether the error is thrown. This is my current approach: it("checking for thrown error", asy ...

horizontal menu consolidation

Web Design <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Hangout</title> <meta http-equiv="Content-Type" content="text ...

Determine the image that was clicked using Jquery Validate

I am working on a form that contains 5 icons, and I need to validate which icon has been clicked. I initially considered adding a hidden text box and implementing a rule to check its value. While this works on form submission, I am facing an issue with cle ...

Tampermonkey's .click() function appears to be malfunctioning

I am attempting to automate clicking a button in Tampermonkey, but for some reason the code is not working as expected. Interestingly, when I manually run the code in the console, it functions perfectly. Here is the snippet: $(document).ready(function() ...

Encounter the "Error: Source 'cloudsTileLayer-RasterSource' not found" message while trying to integrate a weather tile layer into Azure Maps

I have been working on a React application that utilizes the React-Azure-Maps npm package. My current challenge involves creating a weather layer, which I believe shares similarities with the sample code provided for layers. The code snippet responsible f ...

Unique patterns on a 3D model in Three.js

I am attempting to replicate the look of the model shown in this photo https://i.sstatic.net/IDLaV.png How can I remove the strange lines from the model while rotating it? You can observe these lines when rotating the model on this link . What seems to be ...

Having trouble with refreshing the div content when using jQuery's $.ajax() function

My goal is to refresh a div that has the id #todos after saving data in the database. I attempted to use .load() within $.ajax() $('.todo--checkbox').change(function () { let value = $(this).data('value'); $.ajax({ url: ...

Add characters to div using JavaScript

I am curious about which framework, if any, would be most effective for capturing keystrokes and adding them to an HTML element such as a "p" element. My goal is to allow the client to type something on the keyboard and have it immediately displayed in the ...

Identifying identical web elements using Python Selenium through related attributes

Having attempted to solve this problem for the third time, I find myself unable to come to a final solution on my own. I would greatly appreciate it if someone could offer their insights on the issue at hand. Let's consider the following HTML structu ...

Issue with EnumDeserializer in jackson-mapper 1.9.12 version

I'm currently working on a scenario where I need to map a String to an enum Object using the Jackson ObjectMapper.readValue(String,Class) API. The issue arises when my JSON string contains a Task Object with an Action enum defined as follows: public ...