Skip to main content

Color Picker Using jQuery Source Code


In this tutorial we will create a Color Picker using jQuery. This code will generate a color picker application when user launch the app. The code use a jQuery plugin to bind all the textboxes in the page instantly. And to manipulate each properties with a given formula. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will be using jQuery to simplify and the client-side scripting of HTML. The use of jQuery is to provide an easy way to use JavaScript and Ajax API that works on a lot of different type of browsers.
.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

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

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">Color Picker Using jQuery Source Code</h3>
  18. <hr style = "border-top:1px dotted #ccc;"/>
  19. <div class = "col-md-5">
  20. R<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "red"/>
  21. <br />
  22. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_red" />
  23. <br /><br />
  24. G<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "green"/>
  25. <br />
  26. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_green" />
  27. <br /><br />
  28. B<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "blue"/>
  29. <br />
  30. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_blue" />
  31. <br /><br />
  32. </div>
  33. <div class = "col-md-6" style = "border:1px solid #000; height:300px; transition: background 100ms; float:right;" id = "result"></div>
  34. </div>
  35. </div>
  36. </body>
  37. <script src = "js/jquery-3.2.1.min.js"></script>
  38. <script src = "js/script.js" ></script>
  39. </html>

Creating the Script

This code contains the script of the application. This code will change the color of an object by manipulating the given forms. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  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. });

There you have it we successfully created a Color Picker 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.