Getting started in EJ2 JavaScript Check box control
23 Nov 20268 minutes to read
This section explains the steps required to create a simple Essential® JS 2 CheckBox and demonstrate the basic usage of the CheckBox control in a JavaScript application.
Dependencies
The list of dependencies required to use the CheckBox component in your application is given below:
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-baseSetup 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 CheckBox control.
Control Initialization
The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts
Add Syncfusion® CheckBox 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 scripts are required dependency packages for the CheckBox component.
Styles(CSS):
https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/check-box/index.cssScripts (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-buttons/dist/global/ej2-buttons.min.jsAdding CheckBox component to the application
Add the CheckBox element and initiate the Syncfusion® JavaScript CheckBox control in the index.html by using following code.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Essential JS 2</title>
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/check-box/index.css" rel="stylesheet" />
<!-- Essential JS 2 Base's global script (Dependency Script) -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<!-- Essential JS 2 CheckBox's global script (Control Script) -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<!--element which is going to render-->
<input id="element" type="checkbox"/>
<script>
ej.base.enableRipple(true);
// Initialize CheckBox control.
var checkbox = new ej.buttons.CheckBox({ label: 'Default' });
// Render initialized CheckBox.
checkbox.appendTo('#element');
</script>
</body>
</html>#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
li {
list-style: none;
}
.e-checkbox-wrapper {
margin-top: 18px;
}
.e-checkicon.e-checkbox-wrapper .e-frame.e-check::before {
content: '\e790';
}
.e-checkicon.e-checkbox-wrapper .e-check {
font-size: 8px;
}
.e-checkicon.e-checkbox-wrapper .e-frame.e-check {
background-color: white;
border-color: grey;
color: grey;
}
.e-checkicon.e-checkbox-wrapper:hover .e-frame.e-check {
background-color: white;
border-color: grey;
color: grey;
}
.e-checkicon.e-checkbox-wrapper .e-checkbox:focus + .e-frame.e-check {
background-color: white;
border-color: grey;
box-shadow: none;
color: grey;
}
.e-checkicon.e-checkbox-wrapper .e-ripple-element {
background: grey;
}Indeterminate State
The CheckBox indeterminate state can be set through indeterminate property. CheckBox indeterminate state masks the real value of CheckBox visually. The Checkbox cannot be changed to indeterminate state through the user interface, this state can be achieved only through the property.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Essential JS 2</title>
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/check-box/index.css" rel="stylesheet" />
<!-- Essential JS 2 Base's global script (Dependency Script) -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<!-- Essential JS 2 CheckBox's global script (Control Script) -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<!--element which is going to render-->
<input type="checkbox" id="checkbox1"/>
<input type="checkbox" id="checkbox2"/>
<input type="checkbox" id="checkbox3"/>
<script>
ej.base.enableRipple(true);
//checked state.
var checkbox = new ej.buttons.CheckBox({ label: 'Checked State', checked: true });
checkbox.appendTo('#checkbox1');
//unchecked state.
checkbox = new ej.buttons.CheckBox({ label: 'Unchecked State' });
checkbox.appendTo('#checkbox2');
//indeterminate state.
checkbox = new ej.buttons.CheckBox({ label: 'Indeterminate State', indeterminate: true });
checkbox.appendTo('#checkbox3');
</script>
</body>
</html>#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
li {
list-style: none;
}
.e-checkbox-wrapper {
margin-top: 18px;
}
.e-checkicon.e-checkbox-wrapper .e-frame.e-check::before {
content: '\e790';
}
.e-checkicon.e-checkbox-wrapper .e-check {
font-size: 8px;
}
.e-checkicon.e-checkbox-wrapper .e-frame.e-check {
background-color: white;
border-color: grey;
color: grey;
}
.e-checkicon.e-checkbox-wrapper:hover .e-frame.e-check {
background-color: white;
border-color: grey;
color: grey;
}
.e-checkicon.e-checkbox-wrapper .e-checkbox:focus + .e-frame.e-check {
background-color: white;
border-color: grey;
box-shadow: none;
color: grey;
}
.e-checkicon.e-checkbox-wrapper .e-ripple-element {
background: grey;
}