JavaScript - Basic Color Picker
Submitted by razormist on Sunday, March 31, 2019 - 11:58.
In this tutorial we will create a Basic Color Picker using JavaScript. This code can generate a color picker application in order to change the color of an object. The code itself use a jQuery plugin to manipulate all the textboxes the page without instantly, also preventing the page to refresh every time there is a changes. 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.
.
There you have it we successfully created a Basic Color Picker using 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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- </div>
- </div>
- </nav>
- <div class="row">
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #000;"/>
- <div class="col-md-6">
- <br style="clear:both;"/>
- <br />
- </div>
- <div class="col-md-12">
- <div class="form-inline" style="text-align:center;">
- <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_red" />
- <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_green" />
- <input type="number" class="form-control" value="0" min="0" max="255" size="4" id="txt_blue" />
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will automatically change the object color by manipulating the textboxes. 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- $(document).ready(function(){
- $input_text = $('input[type="number"]');
- $input_text.change(function(){
- $txt_red = $('#txt_red').val();
- $('#red').val($txt_red);
- $txt_green = $('#txt_green').val();
- $('#green').val($txt_green);
- $txt_blue = $('#txt_blue').val();
- $('#blue').val($txt_blue);
- $('#result').css('background', 'rgb(' + $txt_red +', ' + $txt_green + ', ' + $txt_blue + ')');
- $rgb = 'rgb(' + $txt_red + ', ' + $txt_green + ', ' + $txt_blue + ')';
- $hex = rgb2hex();
- $('#hex').val($hex);
- });
- });
Add new comment
- 120 views