Alter the hue of a solitary shade in the linear gradient

Is there a way to modify the initial color in a CSS linear gradient solely using Javascript?

div {
    background: -webkit-linear-gradient(top, #F6F6F6, #E9E9E9)
}

I am specifically looking to alter the hue "#F6F6F6" while leaving the other color unchanged.

It is important that the solution is written in pure Javascript rather than utilizing jQuery.

Answer №1

To update the styling of the div, simply replace the existing class with a new one that includes the desired attributes.

Check out the code in action on this Fiddle.

In this example, I recommend adding a new class 'divs' to all div elements along with the 'linearGradient' class for consistent styling.

<div class='divs linearOne'>
</div>

I've included JQuery for handling button click events:

var divs = document.getElementsByClassName('divs');

var toggle = false; //used to toggle between

document.getElementById('clickme').addEventListener("click", function (event) {
  if(!toggle){ 
      divs[0].classList.remove('linearOne')
      divs[0].classList.add('linearTwo');
      toggle = true;
    } else {
      divs[0].classList.add('linearOne')
      divs[0].classList.remove('linearTwo');
      toggle=false;
      }

})
.linearOne {
  width:100px;
  height:100px;
  background: -webkit-linear-gradient(top, #F6F6F6, #E9E9E9)
}

.linearTwo {
  width:100px;
  height:100px;
  background: -webkit-linear-gradient(top, #000000, #F6F6F6)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='divs linearOne'>

</div>

<button id='clickme'>
Toggle Gradient
</button>

Answer №2

To modify the background of your <div> using JavaScript when it has an id, you can easily change it to (1) a different gradient, (2) a specific color, or (3) remove the background altogether with just a few lines of code:

document.getElementById('anotherGradient').style.background = "-webkit-linear-gradient(top, #00E9E9, #F6F600)";
document.getElementById('greyBackground').style.background = "#E9E9E9";
document.getElementById('noBackground').style.background = "none";
div {
    background: -webkit-linear-gradient(top, #F6F6F6, #E9E9E9)
}
<div id="anotherGradient">
    This DIV should have a different gradient
</div>
<div id="greyBackground">
    This DIV should have a grey background but no gradient
</div>
<div id="noBackground">
    This DIV should not have any background at all
</div>
<div>
    This DIV should have the original gradient
</div>
<div>
    This DIV should have the original gradient
</div>
(see also this Fiddle)

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

Blurring the edges of a div container

I have successfully faded the top of a div, but I am struggling to achieve the same effect for the bottom. I attempted to reverse the CSS properties used for fading the top, but it does not seem to be working as expected. HTML: <div class="container-c ...

What is the best way to display a button only when a different element is in focus?

I am working on a feature where each row consists of two text inputs and a button. The goal is to show the button when a user focuses on one of the input fields, and hide it again when they lose focus. I have come up with this solution: const Input = ({inp ...

How can I troubleshoot an image not appearing in Bootstrap within a Laravel Blade file while using PHPStorm?

Forgive me if this sounds like a silly question: I attempted to use the following src attribute in an img tag to show an image on a website created with Bootstrap, while using phpstorm with a laravel blade file: src="C:\Users\MAHE\Pictures&b ...

How come the words are clear, but the background remains unseen?

My markup looks like this: <div class="one">Content</div> <div class="two"></div> I have the following CSS styles: .one, .two { height: 20px; } .one { background-color: #f00; } .two { background-color: #0f0; margi ...

Vertical alignment of buttons in Cordova App Rate

Currently, I am utilizing phonegap along with the app rate plugin. When I execute the code: AppRate.promptForRating(true); All buttons are appearing in a singular line. Despite there being 3 buttons available, the text on the buttons is getting truncate ...

What is the most effective way to organize an array according to a key function that is computationally intensive?

After posting this question regarding sorting an array with a key function, it became evident that utilizing a comparison function was inevitable. The challenge at hand includes: Having a resource-intensive key function that needs to be transformed into ...

Alter the color of Bootstrap navigation bar connectors

It seems that I'm experiencing a challenge with the code as I am unable to modify the color of the navigation bar links from black to white. .navbar-nav .nav-link active { color: #FFFFFF; } body { color: #FFFFFF } .collapse .navbar-collapse { ...

When running the command `npx create-react-app client`, an error is thrown stating "Reading properties of undefined is not possible (reading 'isServer')."

Installing packages. Please wait while the necessary packages are being installed. Currently installing react, react-dom, and react-scripts with cra-template... Encountered an error: Unable to read properties of undefined (reading 'isSer ...

Storing an array of JSON objects in separate rows within an SQL database table

I need help with inserting data from an API post request into a MySQL table using Express JS. The JSON array contains multiple objects that I want to fill out the rows in the table, but I can't seem to get it right. Any assistance would be appreciated ...

Using CSS GRID for creating layouts with customized grid-template-areas that include empty cells and automatic placement

Struggling with implementing the css grid layout module to showcase a 12-column, 3-row grid. The initial row should only contain two elements, positioned on each side of the grid with empty cells in between using ten periods. Subsequent rows should autom ...

Illuminate the image upon checking the checkbox, as well as when clicking on the image itself

Issue with Implementation <div> <input type="checkbox" id="check" class="check-with-label" /> <label for="check" class="label-for-check">My Label <img class="product-add-image-preview" src="http://ecx.images-amazon.com/images ...

Filtering data with AngularJS upon user clicks

Encountering a perplexing issue at the moment. I have created buttons using ng-repeat and my intention is for them to filter my list when clicked on. However, I am facing an issue where the list fails to filter upon clicking. Here are the buttons: <div ...

Building a Next.js application in a docker container on a local machine using minikube is successful, however, the build fails when attempting to build it on a staging environment

I've encountered a puzzling issue. My next.js app is running in a docker container. It builds successfully on my local Ubuntu machine with minikube and ks8 orchestration, but it gets stuck indefinitely during the build process on the staging setup. T ...

Querying for the presence of an ObjectId in an array in Mongoose

I'm developing a Node.js project that involves two models: User and Project. Below is the Schema for the Project model: const ProjectSchema = new mongoose.Schema({ name: { type: String, maxlength: 50, required: true, } ...

Executing a conditional onClick function when the page loads

I have implemented a php-based authorization system that loads different authorization levels into a JavaScript variable. My goal is to disable certain onclick events based on the user's authorization level. I've come up with the following code ...

Master the Art of Scrollbar Control in Angular!

I am currently developing a chat web application that functions similar to gchat. One of the key features I'm trying to implement is an alert notification when the scrollbar is in the middle of the div, indicating a new message. If the scrollbar is at ...

Creating custom designs for Material UI components

Although not a major issue, there is something that bothers me. I am currently using react, typescript, and css modules along with . The problem arises when styling material ui components as I find myself needing to use !important quite frequently. Is th ...

Populate a pair of div elements using just one AJAX request

Currently, I am attempting to use the load() function to ajaxly load two separate divs, #follow-x and #follow-y, by clicking a button. The approach I have taken is as follows within the success function of my code snippet. However, this method does not see ...

Looking to place a <div> element on a new line immediately after the <header> tag?

I experimented with this code and attempted to modify the display properties of the header and div tags. However, I encountered an issue where the image in the next div tag appeared in the top left corner. My goal is to position it on the next line to th ...

Passing form values from JavaScript to a JSP page - a comprehensive guide

I am working on a sample HTML page that includes inline JavaScript for calculating geocodes and retrieving latitude and longitude values. My question is, how can I submit all the form values along with the latitude and longitude returned from the showlocat ...