How to Create a Color Picker in HTML using jQuery

How to Create a Color Picker in HTML using jQuery

Introduction

In this tutorial we will create a How to Create Color Picker in HTML using jQuery. This tutorial purpose is to give you a way of change your background color dynamically. This will thoroughly take the basic parts of creating the color picker app with the help of jQuery . I will provide a sample program to show the actual coding of this tutorial. 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 interface of the background and html forms. 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 Color Picker in HTML</h3>
  18. <hr style="border-top:1px dotted #000;"/>
  19. <div class="col-md-3">
  20. <div class="col-md-12">
  21. <div class="form-inline" style="text-align:center;">
  22. <label>R</label>
  23. <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_red" />
  24. <label>G</label>
  25. <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_green" />
  26. <label>B</label>
  27. <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_blue" />
  28. </div>
  29. </div>
  30. </div>
  31. <div class="col-md-6">
  32. <div class="col-md-12" style="border:1px solid #000; height:250px; transition: background 100ms;" id="result"></div>
  33. <br style="clear:both;"/>
  34. <br />
  35. </div>
  36.  
  37. </div>
  38. </div>
  39. </body>
  40. <script src = "jquery-3.1.1.js" ></script>
  41. <script src = "script.js" ></script>
  42. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to calibrate the color of the background dynamically. 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_text = $('input[type="number"]');
  3. $input_text.change(function(){
  4. $txt_red = $('#txt_red').val();
  5. $('#red').val($txt_red);
  6. $txt_green = $('#txt_green').val();
  7. $('#green').val($txt_green);
  8. $txt_blue = $('#txt_blue').val();
  9. $('#blue').val($txt_blue);
  10. $('#result').css('background', 'rgb(' + $txt_red +', ' + $txt_green + ', ' + $txt_blue + ')');
  11. $rgb = 'rgb(' + $txt_red + ', ' + $txt_green + ', ' + $txt_blue + ')';
  12. $hex = rgb2hex();
  13. $('#hex').val($hex);
  14. });
  15. });

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. And then we bind all the textboxes using the jQuery method and use the change() function, this function can allow you to manipulate an element when a bind element has been change. To change the background color we just simply append the value of the textboxes into the css color value.

Output:

The How to Create Color Picker in HTML using jQuery 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 Color Picker in HTML using jQuery. 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