Dynamically adding a CSS gradient to an element with JQuery

Check out my HSL color picker on JS Bin

I have created a simple HSL color picker that dynamically applies a gradient to a range input type upon DOM ready and changes to update the picker.

// Code Update Setup
$(".cpick-code-hsl").on('change keyup', function() {
  $(this).val( "hsla(" + $(".cpick-hue-text").val() + ", " + $(".cpick-s-text").val() + ", " + $(".cpick-l-text").val() + ", " + $(".cpick-a-text").val() + ")");

  // Apply as body background
  $(".head").css({
    "background": $(".cpick-code-hsl").val()
  });

  $(".cpick-code-rgb").val( $(".head").css("backgroundColor") );

  // Alpha Saturation
  $(".cpick-s").css({
    "background": "linear-gradient(to right, #7f7f80 0%," + "hsl(" + $(".cpick-hue").val() + "," + $(".cpick-s").val() + "%," + $(".cpick-l").val() + "%)" + " 100%)"
  });
  // Alpha Lightness
  $(".cpick-l").css({
    "background": "linear-gradient(to right, #000000 0%," + "hsl(" + $(".cpick-hue").val() + "," + $(".cpick-s").val() + "%," + $(".cpick-l").val() + "%) 50%,#ffffff 100%)"
  });
  // Alpha Preview
  $(".cpick-a").css({
    "background": "linear-gradient(to right, rgba(51,51,51,0) 0%," + "hsl(" + $(".cpick-hue").val() + "," + $(".cpick-s").val() + "%," + $(".cpick-l").val() + "%)" + " 100%)"
  });
});

I found the Gradientz plugin, but it doesn't work in Firefox. Does anyone know of a simpler and more effective solution?

Answer №1

Are you able to access a stylesheet or include a <style> element? It would simplify things, boost performance, and clean up your code if you create a CSS class and then use jQuery to add that class to your element.

Check out this JSFiddle link for reference

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

Retrieve the value from the URL by parsing it using JSON

My goal is to extract the asciiName from a specific URL: http://services.gisgraphy.com/geoloc/search?lat=21.283300399780273&lng=72.9832992553711&radius=10000<br> I am utilizing ajax jsonp for this task. Below is the complete code snippet ...

Tips for updating images with HTML and CSS?

Sorry if this question seems strange, but I have a doubt that I need clarification on. Regarding assumptions: If I have 5 images, is it possible to change each image one by one (like a slider) using only HTML and CSS styles, without relying on jQuery or J ...

the power of using keywords and prototypes

Greetings! I am currently delving into the realm of JavaScript, hailing from a C++ background. The transition has proven to be quite perplexing for me. Below is a snippet of code that I have been troubleshooting: var someArray = []; nameCompare = function ...

How do I activate Angular/Backbone/jQuery intellisense in VSCode for a project that does not have a package.json file?

Currently, I find myself at a standstill trying to enable code autocompletion for popular libraries like BackboneJS, Angular, and jQuery. Unfortunately, the recommended method provided by the VS Code official documentation is not accessible to me (which in ...

JavaScript and HTML code for clipboard functionality without the need for Flash

My challenge lies in creating a grid with numerous columns and data. The user has expressed the desire for a copy to clipboard button that will allow them to easily copy the data. Can anyone suggest ways to implement Copy to Clipboard functionality withou ...

Using React JS to invoke PHP script upon onClick event

Struggling to figure out the process of calling a PHP script through an AJAX call from a React JS script or when a submit button is clicked in the React component. It was a breeze with normal HTML and jQuery, but transitioning to React has proven challengi ...

Iterate through each row of the PHP array

Hey there, I have two sets of code that I'd like to share with you. The first one is an Ajax snippet: /* Loop and Retrieve Data from Database to Generate a Table */ $(document).ready(function () { $('#btngenerate').click(function(e){ ...

Can a single file in NextJS 13 contain both client and server components?

I have a component in one of my page.tsx files in my NextJS 13 app that can be almost fully rendered on the server. The only client interactivity required is a button that calls useRouter.pop() when clicked. It seems like I have to create a new file with ...

Design a dynamic dropdown feature that is triggered when the chip element is clicked within the TextField component

Currently, I am facing difficulties in implementing a customized dropdown feature that is not available as a built-in option in Material UI. All the components used in this project are from Material UI. Specifically, I have a TextField with a Chip for the ...

When the user modifies the input, React fails to update the component state

Currently, I am utilizing React in conjunction with express to server-side render the views for my project. However, I have encountered a minor setback. Within one of my components, users are directed after their initial login. Here, they are prompted to ...

Loading jQuery via JavaScript in the head section of the HTML document

I'm currently developing an application using jQuery and jQuery Mobile. I have a script in the head section that dynamically loads both libraries. However, my body contains a script that relies on jQuery ($) and is unable to access it because it loads ...

Linking to CSS in SharePoint Asset Library results in display issues

Issue encountered: Styles not rendering correctly when linking to CSS in SharePoint. Interestingly, placing the CSS directly on the page or linking it through BitBucket, GitHub, or a CDN results in proper rendering. Is hosting Intranet design files like ...

Refresh your page with JQUERY and location.reload() callback

Is it possible to have a callback after using location.reload() with JQUERY to refresh the page when a user changes language? Although it refreshes the page, I am wondering if it might be possible to have a callback function as well. I would appreciate a ...

Securing a namespace using passport js: A step-by-step guide

Imagine I have set up a specific route with the following namespace: app.use('/registered', userRoute); Recently, I wrote the following passportjs function: app.get('/logged/dashboard', function (req, res) { if (req.user === undefi ...

Can we apply query projection to a collection that already includes a $elemMatch projection?

I have a question regarding limiting the fields in a matching subdocument array using the $elemMatch projection. It seems like it returns all fields of the subdocuments that match, regardless of any specified query projections. But is there a way to restr ...

Retrieving the 'red' pixel data from the texture rendered with gl.texImage2D

My objective is to transfer a Float32array to my fragment shader using a texture in order to process the data within the shader and then send it back to JavaScript. Since the data is not in the form of an image, I opted to transmit it as 'gl.R32F&apos ...

Transitioning from one bootstrap modal to another in quick succession may lead to unexpected scrolling problems

I'm facing a challenge with two modals where scrolling behavior becomes problematic when transitioning from one to the other. Instead of scrolling within the modal itself, the content behind it is scrolled instead. In order to address this issue, I im ...

Problems with the main title formatting in the header

I am currently working on a new website and have encountered an issue with the header design. The Mainline "PersIntra" is overlapping the "log out button" box, which I would like to be positioned beside it instead. Despite my efforts with CSS and nesting ...

Dynamically enhance the JSON root with additional value

Oh dear, I'm feeling quite perplexed right now. I created a JSON element in the following way: var planet = {"continent": []}; planet.continent.push({name: "Asia", flag: "yes", countries: []}); planet.continent[0].countries.push({name: "Japan", capi ...

Is there a way to stream an mp3 file in a Node.js REPL on Replit?

I have an MP3 file that I want to play when a button is clicked. However, I suspect that I am not correctly serving the file to the server. The following code snippet is from my project on Replit.com: const app = require('express')(); const http ...