J Query Easy Color Chooser - ideal for choosing background colors

As I venture into the world of HTML coding, I am struggling to implement a jQuery simple color picker to change the background color. Despite my efforts with various color pickers, I can't seem to make the background and display box respond correctly.

Click here for more information on the jQuery UI Slider Colorpicker.

The code I have utilized from the website can be found below:

<!doctype html>
<html lang='en'>
<head>
    <!-- Source https://jqueryui.com/slider/#colorpicker, retrieved Feb 2018 
        added missing 'https:' to line 7
        removed local call to /resources/demos/style.css line 8-->
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>jQuery UI Slider - Colorpicker</title>
    <link rel='stylesheet' href='https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'>
    <style>
        #red, #green, #blue {
            float: left;
            clear: left;
            width: 300px;
            margin: 15px;
        }
        #swatch {
            width: 120px;
            height: 100px;
            margin-top: 18px;
            margin-left: 350px;
            background-image: none;
        }
        #red .ui-slider-range { background: #ef2929; }
        #red .ui-slider-handle { border-color: #ef2929; }
        #green .ui-slider-range { background: #8ae234; }
        #green .ui-slider-handle { border-color: #8ae234; }
        #blue .ui-slider-range { background: #729fcf; }
        #blue .ui-slider-handle { border-color: #729fcf; }
    </style>

    <script src='https://code.jquery.com/jquery-1.12.4.js'></script>
    <script src='https://code.jquery.com/ui/1.12.1/jquery-ui.js'></script>
    <script>
       // JavaScript functions for handling color selection
    </script>

<body>
    <p>Customize your colors using the interactive sliders:</p>
    <div id='red'></div>
    <div id='green'></div>
    <div id='blue'></div> 
    <div id='swatch' class='ui-widget-content ui-corner-all'></div>




</body>
</html>

Answer №1

If you want to achieve that effect, all you need to do is substitute #swatch with body within the refreshSwatch() function:

$(function() {
          function hexFromRGB(r, g, b) {
            var hex = [
              r.toString(16),
              g.toString(16),
              b.toString(16)
            ];
            $.each(hex, function(nr, val) {
              if (val.length === 1) {
                hex[nr] = '0' + val;
              }
            });
            return hex.join('').toUpperCase();
          }

          function refreshSwatch() {
            var red = $('#red').slider('value'),
              green = $('#green').slider('value'),
              blue = $('#blue').slider('value'),
              hex = hexFromRGB(red, green, blue);
              // $('#swatch').css('background-color', '#' + hex);  <-- replace this
              $('body').css('background-color', '#' + hex);     // <-- with this
          }
          $('#red, #green, #blue').slider({
            orientation: 'horizontal',
            range: 'min',
            max: 255,
            value: 127,
            slide: refreshSwatch,
            change: refreshSwatch
          });
          $('#red').slider('value', 255);
          $('#green').slider('value', 140);
          $('#blue').slider('value', 60);
        });
<!doctype html>
<html lang='en'>

  <head>
    <!-- Source  https://jqueryui.com/slider/#colorpicker, retrieved Feb 2018 
            added missing 'https:' to line 7
            removed local call to /resources/demos/style.css line 8-->
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>jQuery UI Slider - Colorpicker</title>
    <link rel='stylesheet' href='https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'>
    <style>
      #red,
      #green,
      #blue {
        float: left;
        clear: left;
        width: 300px;
        margin: 15px;
      }

      #swatch {
        width: 120px;
        height: 100px;
        margin-top: 18px;
        margin-left: 350px;
        background-image: none;
      }

      #red .ui-slider-range {
        background: #ef2929;
      }

      #red .ui-slider-handle {
        border-color: #ef2929;
      }

      #green .ui-slider-range {
        background: #8ae234;
      }

      #green .ui-slider-handle {
        border-color: #8ae234;
      }

      #blue .ui-slider-range {
        background: #729fcf;
      }

      #blue .ui-slider-handle {
        border-color: #729fcf;
      }

    </style>

    <script src='https://code.jquery.com/jquery-1.12.4.js'></script>
    <script src='https://code.jquery.com/ui/1.12.1/jquery-ui.js'></script>

  </head>

  <class='ui-widget-content' style='border:0;'>
    <p class='ui-state-default ui-corner-all ui-helper-clearfix' style='padding:4px;'>
      <span class='ui-icon ui-icon-pencil' style='float:left; margin:-2px 5px 0 0;'></span>
      Simple Colorpicker
    </p>
    <div id='red'></div>
    <div id='green'></div>
    <div id='blue'></div>
    <div id='swatch' class='ui-widget-content ui-corner-all'></div>



    </body>

</html>

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

A div element featuring a border with transparent edges on the top right and bottom left corners

Does anyone know how to code the border for the upper right corner and lower left corner of the box (as shown in the image below)? I would really appreciate some help. Thank you in advance! This is the HTML <div class="carouselle"> <div clas ...

Preserve the aspect ratio of a div even when the height changes dynamically

I was intrigued by the idea of creating a square div using only CSS, where its height adjusts dynamically and its width scales based on that height. In the snippet below, you can see that the container's height adapts based on its content. The red ...

How to Style CSS specifically for Internet Explorer?

I'm dealing with a simple div that has a 2px thick border and absolute positioning, but it's hidden until its parent element is hovered over. The issue arises in IE due to the box model, causing the position of this div to be somewhat off in IE c ...

Return to home page

Could a button or link be created on an HTML page using HTML/JavaScript that directs to the URL saved as 'homepage,' similar to clicking on the home icon in Internet Explorer? Warm regards ...

The CSS rotation effect causes the element to spin and twirl

I've been struggling once again to grasp the concept of CSS animations, particularly the rotate animation. I'm trying to create an effect where an image rotates back and forth on hover, but for some reason it only starts rotating after shifting t ...

Displaying from the left side to the right side

Having some issues with sliding from the left to right div that has display:none. <div class="second"> <li><a id="sale_man" rel="showinfo"><?php echo $lang['sale_man'];?></a></li> <li><a id="purch_man ...

Displayed information on hover

https://i.sstatic.net/XVQWG.jpg I've been searching high and low for a simple textbox that appears when you hover over an image, positioned either to the left or the right. I don't need anything too fancy - just a rectangular box with a backgrou ...

Using a for-loop to read inputs from a database in PHP

I am seeking assistance with a problem I am encountering on an input page (the modified user profile). On this page, I have a section for addresses and need to implement a loop where each set of inputs displays the information for each address individually ...

Creating a List with Sublists that are displayed when hovering over the parent List is a key element of effective design

Hovering over 'View Rows' should open up both New Records and Old Records <div> <li>Add Rows</li> <li>DeleteRows</li> <li>View Rows <ul> <li>View New Records</li ...

Steps for altering the primary color in PrimeNG__Changing the primary color

What is the most effective way to modify the default primary color for primeNG (saga-blue theme)? Altering --primary-color does not have the desired effect, as elements in node_modules/..../theme.css are styled using the main color hex rather than '-- ...

What is the best way to center text in the middle of a button?

Can someone help me center the text in the middle of a button like the image below? I tried using the class provided but it doesn't seem to be working. https://i.sstatic.net/EK0Mf.png <div class="row text-center"> <div class="col-lg-6 a ...

Ways to apply styles to the final element containing a specific class within a nested list using CSS

Styling the final HTML element with a specific css class presents challenges due to certain constraints that must be considered. In the website I'm currently working on, the navigation menu consists of multiple nested lists. When an element is clicke ...

identifying unused class names within an HTML codebase

Is there a way or tool available for detecting unused class names in HTML code? I have some HTML code with old Bootstrap, jQuery, and personalized CSS styles like below: <div class="clear fot-her likes tab-sub">content</div> But I n ...

ngx hierarchical dropdown

Is it possible that the 'disabled' attribute is not a recognized property of the 'ngx-dropdown-treeview' component? The ngxDisabledOnSelector property seems to be malfunctioning in my specific case. This is my code snippet: <ngx-dr ...

Tips for displaying a message in a concealed field with jQuery

I have an input field that saves values for checking a specific condition. When the condition is false, I want to display a message. If the field is hidden, the message will not be displayed. HTML <input type="hidden" id="year1_req_fund_hidden" name= ...

delete local ios css style datetime input

I am facing an issue with the new ios 16 version in Safari where input time and input datetime are being displayed incorrectly. Is there a way to fix this problem? I have tried using -webkit-appearance: none; but it did not work. https://i.sstatic.net/em ...

"Enhance your website with a unique Bootstrap 5 carousel featuring multiple

As a beginner in Bootstrap, I am currently working on an ecommerce template to learn about Bootstrap 5. I am interested in creating a carousel that displays multiple slides at once, like a products slider with left and right arrows. Is this possible in Bo ...

The presence of an iframe disrupts the order of the

I have a website () that utilizes CSS to keep the header, menu, and footer fixed while allowing the content to scroll. Recently, I added a photo page using an iframe and it seems to be working well (although some adjustments are still needed for different ...

Transferring data seamlessly between AJAX and PHP

<?php //Establishing site root variable; ?> <?php require_once("../includes/site_init.php"); ?> <!DOCTYPE HTML> <html lang = "en"> <head> <meta charset = "UTF-8"> <meta name = "viewport" content = "width ...

Adjust the code to enhance the functionality of web components in Sharepoint

I recently came across some code online that I'm trying to modify in order to add an expanding button next to a web part on my Sharepoint site. However, the problem is that by default, all web parts are already expanded and require a click to collapse ...