Skip to main content

jQuery Color Picker

This tutorial is to see how to do simple color picker using jQuery. There are various jQuery plugins providing color picker feature. This tutorial gives a simple example code as a guideline to start writing your own jQuery color picker plugin.
In this example, we are using CSS selectors to show color palette. On clicking each color in the palette we store the selected color into a pointer to be applied to the target DIV. We are using jQuery CSS function to pick and apply colors to the target DIV.


HTML Code for Color Palette:
This is the HTML code for showing color palette to pick colors. It shows four colors with separate DIV tags.
<div class="circular-div" id="blue" onClick="pickColor(this.id);"></div>
<div class="circular-div"  id="red" onClick="pickColor(this.id);"></div>
<div class="circular-div"  id="yellow" onClick="pickColor(this.id);"></div>
<div class="circular-div"  id="aqua" onClick="pickColor(this.id);"></div>  
<div id="target" onClick="applyColor();">Click to Apply Color</div>
<div id="pointer"></div>
And the styles are,
<style type="text/css">
#pointer {
 position: absolute;
 top: 0;
 left: 0;
 height: 20px;
 width: 20px;
 border:#000 1px solid;
 border-radius:50%;
}

.circular-div{padding:20px;width:25px;height:200px;float:left;margin:1px;}
#aqua{background:aqua;}
#yellow{background:yellow;}
#bisque{background:bisque;}
#aquamarine{background:aquamarine;}
#target{float:left;padding:20px;height:200px;width:250px;border:#F0F0F0 1px solid;}
</style>

jQuery Color Picker Script:
This script contains very few lines to implement jQuery color picker. We have a pointer to track color on mouse move. On the click event of the color palette, we select the color code and save it to the pointer object. This color code will be applied as the target DIV’s background on its click event.

<script type="text/javascript">
 var demoContent = document.getElementById("demo-content");
 demoContent.addEventListener('mousemove',function(event) {   
  $("#pointer").css({'top':event.pageY+'px','left':event.pageX+'px'}); 
 });
 function applyColor(){
  $("#target").css('background-color',$("#pointer").css('background-color'));
 }
 function pickColor(id){
  $("#pointer").css('background-color',id); 
 }
</script>

Complete Picker code :

<html>
 <body> 
  <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
  <style type="text/css">
  #pointer {
   position: absolute;
   top: 0;
   left: 0;
   height: 20px;
   width: 20px;
   border:#000 1px solid;
   border-radius:50%;
  }
  .circular-div{padding:20px;width:25px;height:200px;float:left;margin:1px;}
  #aqua{background:aqua;}
  #yellow{background:yellow;}
  #bisque{background:bisque;}
  #aquamarine{background:aquamarine;}
  #target{float:left;padding:20px;height:200px;width:250px;border:#F0F0F0 1px solid;}
  </style>
  <div id="demo-content">
   <div class="circular-div" id="aquamarine" onClick="pickColor(this.id);">
    
   </div>
   <div class="circular-div"  id="bisque" onClick="pickColor(this.id);">
     
   </div>
   <div class="circular-div"  id="yellow" onClick="pickColor(this.id);">
   </div>
   <div class="circular-div"  id="aqua" onClick="pickColor(this.id);">
   </div>  
   <div id="target" onClick="applyColor();">Click to Apply Color</div>
   <div id="pointer"></div>
  </div>
  <script type="text/javascript">
   var demoContent = document.getElementById("demo-content");
   demoContent.addEventListener('mousemove',function(event) {   
    $("#pointer").css({'top':event.pageY+'px','left':event.pageX+'px'}); 
   });
   function applyColor(){
    $("#target").css('background-color',$("#pointer").css('background-color'));
   }
   function pickColor(id){
    $("#pointer").css('background-color',id); 
   }
  </script>
 </body>
</html>
Hope you understand it.If any queries please comment below.Please subscribe to other solutions.Thank you :)

Comments

Popular posts from this blog

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...

Parsing a YAML file

YAML is a configuration file format The file itself might look like this: mysql: host: localhost user: root passwd: my secret password db: write-math other: preprocessing_queue: - preprocessing.scale_and_center - preprocessing.dot_reduction - preprocessing.connect_lines use_anonymous: yes You can read it like this: #!/usr/bin/env python import yaml #Import yaml module with open ( "example.yml" , 'r' ) as stream: #open the yaml file in read mode try : myDict = (yaml . load(stream)) #load the file into a dictionary called 'Dict' for section in myDict: print section print myDict[ 'mysql' ] except yaml . YAMLError as exc: print (exc) That's all you need. Now the entire yaml file is in 'MyDict' dictionary. Output: other mysql {'passwd': 'my secret password' , 'host': 'localhost...

Convert HTML to PDF using mPDF library

PDF is format of file to share .Here is code for converting html to PDF using mpdf library (PHP library). //include library path include('mpdf/mpdf.php'); $mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); $mpdf->SetDisplayMode('fullpage'); $mpdf->list_indent_first_level = 0; $mpdf->WriteHTML('<html><head></head><body><h2>Welecome to HTML to PDF </h2></body></html>'); //save the file put which location you need folder/filename $mpdf->Output('test.pdf', 'F'); You can download the library from GitHub. Extract it and paste into your (project folder).