How to Create a Color Picker using jQuery in JavaScript

How to Create a Color Picker using jQuery in JavaScript

Introduction

In this tutorial we will create a How to Create a Color Picker using jQuery in JavaScript. This tutorial purpose is to help you create a simple color picker for your web app. This will thoroughly make use the basic principle of jQuery . I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that uses jQuery plugin to make more interactive feature for your website. I will give my best to provide you the easiest way of creating this program Color Picker. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the color box and multiple inputs. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
  6. </head>
  7. <nav class = "navbar navbar-default">
  8. <div class = "container-fluid">
  9. <div class = "navbar-header">
  10. <a href = "https://sourcecodester.com" class = "navbar-brand">Sourcecodester</a>
  11. </div>
  12. </div>
  13. </nav>
  14. <div class = "row">
  15. <div class = "col-md-3"></div>
  16. <div class = "col-md-6 well">
  17. <h3 class = "text-primary">How to Create a Color Picker using jQuery</h3>
  18. <hr style = "border-top:1px dotted #ccc;"/>
  19. <div class = "col-md-6" style = "border:1px solid #000; height:300px; transition: background 100ms; float:left;" id = "result"></div>
  20. <div class = "col-md-5">
  21. R<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "red"/>
  22. <br />
  23. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_red" />
  24. <br /><br />
  25. G<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "green"/>
  26. <br />
  27. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_green" />
  28. <br /><br />
  29. B<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "blue"/>
  30. <br />
  31. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_blue" />
  32. <br /><br />
  33. </div>
  34. </div>
  35. </div>
  36. </body>
  37. <script src = "js/jquery-3.2.1.min.js"></script>
  38. <script src = "script.js" ></script>
  39. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically adjust the color palette when you move the slider or enter a value in the textbox . To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $input_range = $('input[type="range"]');
  3. $input_range.change(function(){
  4. $red = $('#red').val();
  5. $('#txt_red').val($red);
  6. $green = $('#green').val();
  7. $('#txt_green').val($green);
  8. $blue = $('#blue').val();
  9. $('#txt_blue').val($blue);
  10. $('#result').css('background', 'rgb(' + $red +', ' + $green + ', ' + $blue + ')');
  11. $rgb = 'rgb(' + $red + ', ' + $green + ', ' + $blue + ')';
  12. $hex = rgb2hex();
  13. $('#hex').val($hex);
  14. });
  15.  
  16. $input_text = $('input[type="number"]');
  17. $input_text.change(function(){
  18. $txt_red = $('#txt_red').val();
  19. $('#red').val($txt_red);
  20. $txt_green = $('#txt_green').val();
  21. $('#green').val($txt_green);
  22. $txt_blue = $('#txt_blue').val();
  23. $('#blue').val($txt_blue);
  24. $('#result').css('background', 'rgb(' + $txt_red +', ' + $txt_green + ', ' + $txt_blue + ')');
  25. $rgb = 'rgb(' + $txt_red + ', ' + $txt_green + ', ' + $txt_blue + ')';
  26. $hex = rgb2hex();
  27. $('#hex').val($hex);
  28. });
  29.  
  30. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. In this code we just simply bind the inputs to trigger an event that will adjust the color palette of an element. The function includes some variables and a certain function that will change the color of the bind element.

Output:

The How to Create a Color Picker using jQuery in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Color Picker using jQuery in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment