Employing two contrasting body background colors

Is there a way to have 2 different background colors on a web page?

Check out the code here:

Initially, the background is one color,

https://i.sstatic.net/7p3Gs.png

If you click on a circle, how can you change the background to a different color?

https://i.sstatic.net/9q3T1.png

body {
  background: #353198;
}

Answer №1

To change the background color, simply implement an onclick function on the circle element. Inside this function, add the code snippet below:

document.body.style.backgroundColor = "INSERT_YOUR_COLOR_HERE"; 

Answer №2

Here is a handy tip for you:

document.querySelectorAll(".jacket").addEventListener("click", changeColor());
function changeColor (){
document.body.style.backgroundColor = "blue";
}

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

From PHP to Javascript and back to PHP

I'm currently tackling an issue within my project: My database, utilizing PHP, provides an array containing a collection of JavaScript files that require loading. This list is stored in the $array(php) variable. My task is to extract these source fil ...

What is the method for incorporating a counter variable into a variable's name?

Here's my code, which I know is very inefficient: var slider1 = new Slider("#survey1", { precision: 2, value: 5 }) var slider2 = new Slider("#survey2", { precision: 2, value: 5 }) var slider3 = new Slider("#survey3", { precision: ...

Validating email addresses using regular expressions in JavaScript

After implementing a regex test for the email input, I noticed that an alert is triggered when an incorrect email format is submitted. However, even after dismissing the alert, the email still gets posted to the address specified in the page send_msg.php, ...

What types of data are best suited for storage in localStorage?

During the development of my social app with Vue.js and Vuex store, I am contemplating on the best practices for storing parameters in the browser's localStorage. Currently, I retain the 'token' in localStorage, but should I also store &apos ...

JavaScript: Add an element to the precise middle of an array

Sorry if this is a repeat question, but I couldn't find the answer despite searching. Let's say there's an array of unknown length and you want to insert items at a specific position, such as the center. How can you achieve this? Here&apos ...

What is the best way to responsively center an after pseudo-element above its parent?

Looking to create a dynamic tooltip without any fixed widths or constraints on the parent element. The process seems simple enough, but I'm facing an issue with centering the after element due to an existing transform attribute of transform: translat ...

My jQuery function is designed to run one time only

I need help with creating a function that randomly selects and displays a place when prompted. However, when I click the button, the function only runs once. $(".place").hide(); var randomNumber = Math.floor(Math.random()*44); $("#button").click(funct ...

A JavaScript regular expression for identifying IMDB URLs

Could someone please help me identify the issue with this JavaScript code? "http://www.imdb.com/title/tt2618986/".match("~http://(?:.*\.|.*)imdb.com/(?:t|T)itle(?:\?|/)(..\d+)~i"); I tested it here https://regex101.com/r/yT7bG4/1 and it wo ...

creating a dynamic loop based on an object's key in javascript

Searching for items in a list can be challenging, especially when hardcoded values are used. handleSearch = e => { const q = e.target.value if(q){ const filtered = this.state.data.filter(o => { return Object.values(o).some(val ...

Designing personalized avatars

I am embarking on a new project that involves the creation of multiple custom avatars by users. The idea is to allow users to select images from their inventory and manipulate them within a designated frame. Users should have the ability to move and resize ...

AngularJs Controller with explicit inline annotation

I usually inject dependencies using inline annotations like this angular.module('app') .controller('SampleController',['$scope','ngDependacy',sampleController]); function sampleController($scope,ngDependacy) { ...

Does the rendering of HTML get blocked by CSS in the <head> section like it does with javascript?

While it's common practice to keep JavaScript at the bottom of the HTML document, does the same rule apply for CSS as well? I understand that we can't place external CSS files outside the HTML document. ...

What is the best way to calculate the total of multiple columns using JavaScript and jQuery?

I am looking to modify my table that has two different columns in order to calculate the sum of both. Currently, I can only add up one column using JavaScript, and duplicating the JS code with different value names for the second column is not ideal. How c ...

Dealing with bodyParser errors in Express.js

Whenever I try to upload an image that exceeds my 5mb limit, I get hit with a payload error. Is there a better way to handle payload errors, like sending JSON or HTML responses instead? PayloadTooLargeError: request entity too large at readStream (D:&b ...

Managing Jawbone API OAuth access tokens using node.js (express and passport)

Is there anyone who has successfully completed the Jawbone's OAuth2.0 authentication process for their REST API? I am facing difficulty in understanding how to access and send the authorization_code in order to obtain the access_token as mentioned in ...

Having trouble making z-index work on a child div with absolute positioning

I am attempting to design a ribbon with a triangle div positioned below its parent div named Cart. <div class="rectangle"> <ul class="dropdown-menu font_Size_12" role="menu" aria-labelledby="menu1" style="min-width: 100px; z-index: 0"> & ...

Using Ajax and PHP to Trigger a Forced Download

I am trying to develop a download script that enables the Force Download of JPGs. Below is my PHP script: <?php header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); ...

When positioning 2 divs on top of each other, rotating one by 180 degrees causes it to be considered secondary in the visibility hierarchy. What is the reason behind this?

While delving into the realm of HTML, I stumbled upon a concept that has left me perplexed. Upon stacking two divs on top of each other, I observed an interesting phenomenon where rotating the second div by 180deg (i.e transform:rotateY(180deg)), causes t ...

Obtain gridview row coordinates to create a canvas drawing using Raphael.js

I need to create various shapes based on the status of each row. In order to make it compatible with Internet Explorer, I have integrated the Raphael JavaScript library into my project. The backend is built using ASP.NET and C#. To draw these shapes, I mus ...

Tips on retrieving stored data from asynchronous d3 calls within the document.ready event and passing it to another function

Attempting to save data from an asynchronous call to a variable or object outside of the function has been a challenge. Several methods have been explored to make calls synchronous, such as using callbacks, but none seem to be effective for my particular ...