Getting Started with EJ2 JavaScript Color Picker

21 Nov 20267 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Color picker and demonstrate the basic usage of the Color picker control in a JavaScript application.

Dependencies

The list of dependencies required to use the Color picker component in your application is given below:

|-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-base

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Color picker control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and Scripts

Add Syncfusion® ColorPicker CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base, ej2-splitbuttons, ej2-buttons and ej2-popups scripts are required dependency packages for the ColorPicker component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/color-picker/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/32.1.37/ej2-buttons/dist/global/ej2-buttons.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-splitbuttons/dist/global/ej2-splitbuttons.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js

Inline type

By default, the ColorPicker will be rendered using SplitButton and open the pop-up to access the ColorPicker. To render the ColorPicker container alone and to access it directly, render it as inline. It can be achieved by setting the inline property to true.

The following example demonstrates inline type rendering of the ColorPicker.

[src/index.html]

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <title>Essential JS 2</title>
            <!-- Essential JS 2 ColorPicker's dependency style -->
            <link href="resources/base/material.css" rel="stylesheet" type="text/css"/>
            <link href="resources/buttons/material.css" rel="stylesheet" type="text/css"/>
            <link href="resources/popups/material.css" rel="stylesheet" type="text/css"/>
            <link href="resources/splitbuttons/material.css" rel="stylesheet" type="text/css"/>

            <!-- Essential JS 2 ColorPicker's control style -->
            <link href="resources/inputs/material.css" rel="stylesheet" type="text/css"/>

            <!-- Essential JS 2 ColorPicker's dependency global script -->
            <script src="resources/base/ej2-base.min.js" type="text/javascript"></script>
            <script src="resources/buttons/ej2-buttons.min.js" type="text/javascript"></script>
            <script src="resources/popups/ej2-popups.min.js" type="text/javascript"></script>
            <script src="resources/splitbuttons/ej2-splitbuttons.min.js" type="text/javascript"></script>

            <!-- Essential JS 2 ColorPicker's control global script -->
            <script src="resources/inputs/ej2-inputs.min.js" type="text/javascript"></script>
    </head>
    <body>
            <input type="file" id="fileupload" name="UploadFiles">
        <script>
            // initialize ColorPicker control
            var colorPicker = new ej.inputs.ColorPicker({
                //To render control as inline(flat).
                inline: true,
                showButtons: false
            }, "#element");
        </script>
    </body>
</html>

The showButtons property is disabled in this sample because the control buttons are not needed for inline type. To know about the control buttons functionality, refer to the showButtons sample.

Adding ColorPicker component to the application

To render the ColorPicker container alone and access it directly, set the inline property to true.

The following example demonstrates inline type rendering of the ColorPicker.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>
        <!-- Essential JS 2 ColorPicker's dependency style -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/color-picker/index.css" rel="stylesheet" />

        <!-- Essential JS 2 ColorPicker's dependency global script -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-splitbuttons/dist/global/ej2-splitbuttons.min.js" type="text/javascript"></script>

        <!-- Essential JS 2 ColorPicker's control global script -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
   </head>
   <body>
    <div id='container' style='width: 90%'>
        <input type="color" id="element"/>
    </div>
    <script>
        var colorPicker = new ej.inputs.ColorPicker({
            //To render control as inline.
            inline: true,
            showButtons: false
        }, '#element');
    </script>
</body>
</html>
#container {
    visibility: hidden;
    padding-left: 5%;
    width: 100%;
  }
  #loader {
    color: #008cff;
    font-family: 'Helvetica Neue','calibiri';
    font-size: 14px;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
  }

See Also