There are two ways of ColorPicker initialization:
without specifying a container. Thus, ColorPicker will be initially hidden.
In order to show it, the method show() should be used and the method setPosition() will help to position ColorPicker on the page.
This process can be automatized, by linking one or more inputs to ColorPicker. Clicking an input will show ColorPicker next to it.
To link inputs to ColorPicker you should define a list of inputs at the step of initialization or call the method linkTo() for each input.
The first steps that need to be done for any type of dhtmlxColorPicker's initialization are the following:
If you use dhtmlxColorPicker standalone you need to include 2 files:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="../codebase/dhtmlxcolorpicker.css">
<script src="../codebase/dhtmlxcolorpicker.js"></script>
</head>
<body>
...
</body>
</html>
If you use dhtmlxColorPicker as a part of "dhtmlxSuite" package you need to include 2 files:
<link rel="STYLESHEET" type="text/css" href="../codebase/dhtmlx.css">
<script src="../codebase/dhtmlx.js" type="text/javascript"></script>
To include JS/CSS files of "dhtmlxSuite" package from CDN, you should set direct links to dhtmlx.js and dhtmlx.css files:
<link rel="stylesheet" href="http://cdn.dhtmlx.com/edge/dhtmlx.css"
type="text/css">
<script src="http://cdn.dhtmlx.com/edge/dhtmlx.js"
type="text/javascript"></script>
By setting links in this way you will get the latest version of dhtmlxSuite. To get some particular version, just specify the number of the required version in the link, like this:
<link rel="stylesheet" href="http://cdn.dhtmlx.com/5.0/dhtmlx.css"
type="text/css">
<script src="http://cdn.dhtmlx.com/5.0/dhtmlx.js"
type="text/javascript"></script>
The user needs to create an object where dhtmlxColorPicker will be placed later. In this example the object is a <div> element on page which is placed in the <body> tag:
<div id="colorPicker" style="position:absolute;top:150px;left:200px;"></div>
The next step is to create a new dhtmlXColorPicker and place it after the <div> element (object) that we've just created:
var myColorPicker = new dhtmlXColorPicker({
parent : "c1",
color : "#0000ff",
custom_colors : true
});
parameters:
You can find other available parameters in the article Object Constructor.
As it was said before, it is possible to create ColorPicker and link it to several inputs situated on a page.
Firstly, the user should create the inputs, for example:
<input type="text" id="colorPicker_1">
<input type="text" id="colorPicker_2">
<input type="text" id="colorPicker_3">
<input type="text" id="colorPicker_4">
And then, the following code creates a ColorPicker object and links it to the created inputs:
myColorPicker = new dhtmlXColorPicker("СolorPicker_1",
"СolorPicker_2","СolorPicker_3","СolorPicker_4");
or
myColorPicker = new dhtmlXColorPicker();
myColorPicker.linkTo("СolorPicker_1");
myColorPicker.linkTo("СolorPicker_2");
myColorPicker.linkTo("СolorPicker_3");
myColorPicker.linkTo("СolorPicker_4");
The ColorPicker will be activated when the user clicks somewhere in the input.
When the necessary color is chosen and the button "Select" is pressed, the ColorPicker closes. The code of the chosen color is displayed in the input (for example, #77e5cd).
Back to top