Switch between different RGB background colors

I'm currently working on a project where I want to toggle the background color from RGB to a solid color. My approach involves using Change inner HTML to switch the background color, but I've been facing some difficulties in getting it to work correctly. Since I'm relatively new to using the change inner HTML method, I might be missing something crucial. Here's what I have so far:

Below is my current code snippet for the HTML:

<!DOCTYPE html>
<html>
 <head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="script.js"></script>
 </head>
 <body oncontextmenu="return false" onkeydown="return false;" onmousedown="return false;">
  <div id="toggle">
   <div class="wrapper">
    <button onclick="tog()">Toggle</button>
   </div>
  </div>
 </body>
</html>

Here's the corresponding CSS code:

.wrapper {
  margin:0px;
  height: 200%;
  width: 100%;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  position: absolute;
  background: linear-gradient(
    124deg,
    #ff2400,
    #e81d1d,
    #e8b71d,
    #e3e81d,
    #1de840,
    #1ddde8,
    #2b1de8,
    #dd00f3,
    #dd00f3
  );
  background-size: 1800% 1800%;

  -webkit-animation: rainbow 18s ease infinite;
  -z-animation: rainbow 18s ease infinite;
  -o-animation: rainbow 18s ease infinite;
  animation: rainbow 18s ease infinite;
}

@-webkit-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}
@-moz-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}
@-o-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}
@keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}

Lastly, here's the JavaScript code that I've implemented:

let rgb;

function tog(){
 if(rgb = <div class="wrapper">
    <button onclick="tog()">Toggle</button>
   </div>){
 toggle.innerHTML = <button onclick="tog()">Toggle</button>;
 }else{
 toggle.innerHTML = <div class="wrapper">
    <button onclick="tog()">Toggle</button>
   </div>;
}
}

Answer №1

To achieve the desired solid green color, we can define a class named solid-color:

.solid-color{
  background: green;
}

Your JavaScript syntax needs correction

Here is a code snippet that may help you reach your objective:

let wrapper = document.querySelector('.wrapper');

function toggleColor() {
  if (wrapper.classList.contains('solid-color')) {
    wrapper.classList.remove('solid-color');
  } else {
    wrapper.classList.add('solid-color');
  }
}
.wrapper {
  margin: 0px;
  height: 200%;
  width: 100%;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  position: absolute;
  background: linear-gradient(124deg, #ff2400, #e81d1d, #e8b71d, #e3e81d, #1de840, #1ddde8, #2b1de8, #dd00f3, #dd00f3);
  background-size: 1800% 1800%;
  -webkit-animation: rainbow 18s ease infinite;
  -z-animation: rainbow 18s ease infinite;
  -o-animation: rainbow 18s ease infinite;
  animation: rainbow 18s ease infinite;
}

.solid-color {
  background: green;
}

@-webkit-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}

@-moz-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}

@-o-keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}

@keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}
<div id="toggle">
  <div class="wrapper">
    <button onclick="toggleColor()">Toggle</button>
  </div>
</div>

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

The Firebase Cloud Function assigned to a specific region is currently experiencing functionality issues

I'm faced with the challenge of invoking a Firebase Cloud Function that has already been successfully deployed from my Vue.js application. The specific requirement is for the function to be executed in the europe-west3 region. My approach involves cl ...

Issue with PrimeNG Carousel width on mobile devices

Currently, I am in the process of developing a system interface with Angular and PrimeNG specifically for mobile devices. The main requirement is to have a carousel to display a set of cards. After evaluating different options, I decided to utilize the ngP ...

Struggling to customize checkbox design using HTML and CSS

Why am I unable to style HTML checkboxes with CSS? No matter what I try, the appearance of the checkboxes remains unchanged: Here is the HTML code: <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /> <nput type="checkbox" id ...

What is causing the radio button's background color not to change after it is selected?

I've been searching and exploring various solutions, but I'm encountering some difficulties with the following scenario: There are a few restrictions: Cannot utilize Javascript or Jquery. Must be achieved using pure CSS. My objective is to chan ...

Converting text to JSON using JSONP with jQuery's AJAX functionality

Potential Duplicate Question 1 Possible Duplicate Inquiry 2 I have been searching for a definitive explanation on JSONP across various questions but haven't been able to find one yet. For instance, I am attempting to make a cross domain request us ...

Elevate your website design by adding interactive Ripple Buttons using a combination of Javascript and

Recently, I came across a script that allows for an animation to occur when a button is clicked. However, I have encountered an issue where the script does not redirect to a link. (function() { var cleanUp, debounce, i, len, ripple, rippleContainer, rip ...

Encountering a ETIMEDOUT error "ip address" when making a Node.js request

What I'm doing in the code I am currently reading a text file containing approximately 3500 links. Subsequently, I iterate through each link to filter out the relevant ones and make requests to retrieve their status codes, links, and page titles usin ...

Tips for saving and accessing Shopping Cart items using localstorage

As I develop a shopping cart for an e-commerce site, I aim to utilize browser localstorage to store the products. The functions that have been added to my code include: - addToCart(): triggered when the "Add to Cart" button is clicked. - addProduct(): ...

Issue with npm installation leading to missing node_modules directory

When attempting to run npm install . in a local directory, I keep encountering the following errors: npm ERR! install Couldn't read dependencies npm ERR! Darwin 15.2.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "." npm ERR! no ...

Enhancing the Rendering of React's props.children

I have set up my code like this: // Parent.js class Parent extends React.Component { render() { return { this.props.children } } } // Child.js class Child extends React.Component { render() { let message; const greet = ...

Exploring the World with GPS Technology and Coding

Starting off, I am looking to develop a web-based or browser-based application in the near future. The goal is to incorporate a GPS module as part of the interface for a self-hosted application on tablets or laptops, utilizing the data for tracking purpo ...

What could be causing the empty space along the right edge of my website?

I've encountered an issue with my website where there's a white space on the right side of the page, causing horizontal scrolling. I attempted to resolve this by adding overflow: hidden to the CSS, which worked perfectly in Chrome. However, the p ...

Retrieve data from multiple groups of dynamically generated list boxes

I've implemented a form on my website that dynamically adds rows with form fields. Each row consists of a textbox and a multiple select list. I found a helpful tutorial on this topic at: Here is the code for the textbox: <input type="textbox" nam ...

Correlating Mailgun Webhook event with Mailing list email

Recently, I have started using the Mailgun API to send emails and have also begun utilizing their mailing list feature. When sending to a mailing list, such as [email protected], I receive a single message ID. However, when receiving webhook responses, t ...

Transforming HTML code into a structured object

The provided code snippet includes HTML markup that needs to be transformed into a specific format. <html> <head> <title>Hello!</title> </head> <body> <div class=”div1”> <h1>This is a ...

Implementing Twin Modals Positioned Side by Side with Bootstrap

My dilemma involves creating two side-by-side modals using Bootstrap's grid logic. I have successfully implemented a single modal with the following code: <div class="modal fade bd-example-modal-lg" tabindex="-1" id="my_modal" role="dialog" aria- ...

Change the iframe's URL to a different one by clicking on a thumbnail

I am attempting to change the src="" attribute of an iframe when I click on the thumbnails. This is my current progress. HTML <div class="videoPopup"><span class="close">X close</span> <iframe width="600" height="400" src="" fram ...

When combining CSS grids, nesting them can sometimes cause issues with the height layout

Check out the code on jsFiddle .component-container { width: 800px; height: 200px; background-color: lightyellow; border: 1px solid red; padding: 10px; overflow: hidden; } .component-container .grid-container-1 { display: grid; grid-tem ...

Dealing with event function inside object property: What's the best way?

Hello, I am currently using bootstrap-table for a web application where I need to redirect to another URL when a table row is clicked. The "onRowClick" events are managed through an object attribute known as onRowClick, where you assign it a function that ...

What causes json.parse to malfunction? and how can you resolve the issue

My server sends data to my JavaScript code in the format below. {"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxxx\"]", "message": "This is a test"}]} To parse this JSON string, my code executes the f ...