Alter the background color of the body as the input type range value is adjusted

How can I change the background color of the body when the value of an input range changes?

Below is the HTML code:

<div class="range">
    <input type="range" min="1" max="100" value="0" class="slider" id="myRange">
</div>

And here is the corresponding Javascript code:

let slider = document.getElementById("myRange");
slider.onchange = () => {
    document.body.style.background = `linear-gradient(90deg,  #2b2e43 0%,#2b2e43 50%,#ffffff 50.1%,#ffffff 100%);`
}

Answer №1

After making a few tweaks, the primary issue seems to be the unnecessary semi-colon you added to the value in the JS code.

  function updateSlider() {
      document.body.style.background = `linear-gradient(90deg,  #2b2e43 0%,#2b2e43 50%,#ffffff 50.1%,#ffffff 100%)`;
 
  }
<div class="range">
    <input type="range" min="1" max="100" value="0" class="slider" id="myRange" onchange="updateSlider()">
</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

Issues with Carousel Plugin Functionality

**Hey everyone, I could really use some help. As a beginner coder, please forgive any errors in my code. I am currently working on a webpage where I need to incorporate a carousel plugin within a panel body to display the latest photos. The code provided ...

Mastering the art of utilizing callbacks in AngularJS for consuming an API

Having trouble handling data from an API and structuring it effectively before passing it to the controller. I've created a factory that retrieves user data from the API, but the provideAllUserData function is causing issues. Below is my services.js: ...

Is there a way to effortlessly append a slug to my URL in a Node.js platform?

Currently facing challenges with my templates. As I develop an e-commerce platform, I am using a json file containing product details to create cards for each product. Due to the large number of products, creating individual pages for each one is not feas ...

What is the best way to extract this function from the methods section of this Vue component?

I recently came across this Vue component called BarChart.vue on https://github.com/apexcharts/vue3-apexcharts Here is the script section of the component; <script> /* eslint-disable */ export default { name: "BarExample", data: funct ...

When an image is loaded, use Jquery to automatically open a new tab, instead of

I need to implement a function where, on loading an image on a page, a new tab opens and starts downloading a file from a specific URL while keeping the original page open. This task is part of a WordPress site with a jQuery section in the backend. I' ...

"Put Jest to the test by running it with the Express

Currently, I am in the process of learning how to build an API with TypeScript and experimenting with testing it using the Jest framework. When utilizing a basic Express application like app = express() supertest(app) everything works smoothly. However, ...

What could be the reason for axios yielding [object Promise] rather than the actual data?

My issue involves a function that retrieves data from an API. However, when I integrate this function into an EJS template, it returns a promise instead of the desired data. Strangely, when I console.log the data, it displays the correct information. Assi ...

What is the most efficient way to utilize a single connection to interact with MongoDB?

I have a series of functions that are responsible for running various queries. Here is the code I am working with: var MongoClient = require('mongodb').MongoClient; async function createDatabase() { MongoClient.connect(urlMongoDB, function(er ...

What is the reason behind localStorage.getItem consistently returning a string value?

Something strange is happening. In the lib.dom.d.ts file, the type for localstorage.getItem shows as 'string | null', but in my app it always returns a string. Why is this discrepancy occurring? ...

I am seeking a basic illustration of text rotation within the confines of a canvas element. Please note that I

Can someone provide me with a basic demonstration of text rotation using canvas? I'm not interested in any animations, just a straightforward static example. ...

Retrieve information from a JSON object based on the specified value

Here is the JSON data I am working with: { "sales": [{ "manager": "alberic", "surgeon": "Dr Barry Biggins", "amount": "300", "country": "USA", "percent-seller": "30", "antiquity": "June 2017", "d ...

Implementing icon display upon click in a Meteor application

Currently, I am in the process of developing an application using meteor and within one of the templates, I have the following code snippet. <h3> <b> <a class="viewed" href="/jobdetails/{{_id}}">{{title}}</a> </b> ...

Upgrading Xeditable button designs using AngularJS

Have a query. Can you replace the save(submit) button and cancel buttons with links in Xeditable for AngularJS? Appreciate your help ...

Issue with sending emails via Node.js nodemailer

I am encountering an error when trying to run Nodemailer that says "Error: Missing credentials for 'PLAIN'". What steps should I take to resolve this issue? Error: Missing credentials for "PLAIN" at SMTPConnection._formatError var nod ...

Cursor disabling effect in IE 11 causing interference with drop-down options on Twitter Bootstrap 3

Right above a disabled twitter bootstrap 'form-control' input element sits a drop-down list. <div> <select> <option> Option 1 </option> <option> Option 2 </option> <option> Op ...

Exploring the possibilities of Javascript with Lego Mindstorms

My current project involves programming Lego Mindstorms with JavaScript, but I'm struggling to find helpful resources on the topic. Can anyone recommend some good sources? Moreover, I'm unsure of how to make specific actions like turning the whe ...

Calculating the number of characters in each textarea by iterating through them with a

I am currently working on a project where I need to implement a character count feature for each field in my form. Below is a snippet of code from my C# file: List<Document> record= _repository.Document.GetChecklist(entity.checklist, Convert.ToInt32( ...

CSS is not being applied to the HTML select element generated by PHP

Issue Update: The problem I'm facing is not related to dynamic generation, but rather the sheer number of items. To see a demo, click here: plunker I have select boxes on my website with a consistent style: dark background with white text. select { ...

Why does MySQL only retrieve the most recent result?

I am currently working with a simple script, but I seem to have encountered an issue. The script is only pulling the last result from the table named "site" when it should be replacing bad words, banned words, and emojis. Below is the system that I have cr ...

What is the process for importing a file with an .mts extension in a CJS-first project?

Here's a snippet from a fetchin.mts file: import type { RequestInfo, RequestInit, Response } from "node-fetch"; const importDynamic = new Function("modulePath", "return import(modulePath);") export async function fetch(u ...