Groups in EJ2 JavaScript Textbox control
15 May 202324 minutes to read
The following section explains you the steps required to create TextBox with icon
and floating label
.
TextBox:
-
Create a parent div element with the class
e-input-group
-
Place input element with the class
e-input
inside the parent div element.
<div class="e-input-group">
<input class="e-input" name='input' type="text" placeholder="Enter Date"/>
</div>
Floating label:
-
Add the
e-float-input
class to the parent div element. -
Remove the e-input class and add
required
attribute to the input element. -
Place the span element with class
e-float-line
after the input element. -
Place the label element with class
e-float-text
after the above created span element. When you focus or filled with value in the TextBox, the label floats above the TextBox.
Creating the Floating label TextBox, you have to set the
required
attribute to the Input element to achieve the floating label functionality which is used for validating the value existence in TextBox. If you want to render the Floating label TextBox without
required
attribute then refer to the Floating label without required attribute section.
<div class="e-float-input e-input-group">
<input type="text" required/>
<span class="e-float-line"></span>
<label class="e-float-text">Enter Name </label>
</div>
And refer to the following sections to add the icons to the TextBox.
With icon and floating label
Create an icon element as a span with the class e-input-group-icon
, and the user can place the icon in either side of TextBox by adding the created icon element before/after the input.
For the floating label enabled TextBox add the icon element as first or last element inside the TextBox wrapper, and based on the element position it will act as prefix or suffix icon.
ej.base.enableRipple(true);
// To get the all input fields and its container.
var inputElement = document.querySelectorAll('.e-input-group .e-input,.e-float-input.e-input-group input');
// Add 'e-input-focus' class to the input for achive ripple effect when focus on the input field.
for (var i = 0; i < inputElement.length; i++) {
inputElement[i].addEventListener("focus", function () {
var parentElement = this.parentNode;
if (parentElement.classList.contains('e-input-in-wrap')) {
parentElement.parentNode.classList.add('e-input-focus');
} else {
this.parentNode.classList.add('e-input-focus');
}
});
inputElement[i].addEventListener("blur", function () {
var parentElement = this.parentNode;
if (parentElement.classList.contains('e-input-in-wrap')) {
parentElement.parentNode.classList.remove('e-input-focus');
} else {
this.parentNode.classList.remove('e-input-focus');
}
});
}
// Add 'e-input-btn-ripple' class to the icon element for achive ripple effect when click on the icon.
var inputIcon = document.querySelectorAll('.e-input-group-icon');
for (var i = 0; i < inputIcon.length; i++) {
inputIcon[i].addEventListener('mousedown', function () {
this.classList.add('e-input-btn-ripple');
});
inputIcon[i].addEventListener('mouseup', function () {
var element = this;
setTimeout(function () {
element.classList.remove('e-input-btn-ripple');
}, 500);
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 TextBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 TextBox Components" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div class='wrap'>
<div id="input-container">
<h4> TextBox with icons </h4>
<div class="e-input-group">
<input class="e-input" type="text" placeholder="Enter Date"/>
<span class="e-input-group-icon e-input-popup-date"></span>
</div>
<div class="e-input-group e-float-icon-left">
<span class="e-input-group-icon e-input-date"></span>
<div class="e-input-in-wrap">
<input class="e-input" type="text" placeholder="Enter Date"/>
</div>
</div>
<div class="e-input-group e-float-icon-left">
<span class="e-input-group-icon e-input-date"></span>
<div class="e-input-in-wrap">
<input class="e-input" type="text" placeholder="Enter Date"/>
<span class="e-input-group-icon e-input-down"></span>
</div>
</div>
<h4> Floating label with icons </h4>
<div class="e-float-input e-input-group">
<input required type="text" />
<span class="e-float-line"></span>
<label class="e-float-text"> Enter Date </label>
<span class="e-input-group-icon e-input-popup-date"></span>
</div>
<div class="e-float-input e-input-group e-float-icon-left">
<span class="e-input-group-icon e-input-date"></span>
<div class="e-input-in-wrap">
<input required type="text" />
<span class="e-float-line"></span>
<label class="e-float-text"> Enter Date </label>
</div>
</div>
<div class="e-float-input e-input-group e-float-icon-left">
<span class="e-input-group-icon e-input-date"></span>
<div class="e-input-in-wrap">
<input required type="text" />
<span class="e-float-line"></span>
<label class="e-float-text"> Enter Date </label>
<span class="e-input-group-icon e-input-down"></span>
</div>
</div>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.wrap {
box-sizing: border-box;
margin: 0 auto;
padding: 20px 10px;
width: 340px;
}
.e-input-group-icon:before {
font-family: e-icons;
}
.e-input-group .e-input-group-icon.e-input-popup-date { /* csslint allow: adjoining-classes */
font-size:16px;
}
.e-input-group.e-small .e-input-group-icon.e-input-popup-date { /* csslint allow: adjoining-classes */
font-size:14px;
}
.e-input-group-icon.e-input-popup-date:before { /* csslint allow: adjoining-classes */
content: "";
}
.e-input-group-icon.e-input-up:before { /* csslint allow: adjoining-classes */
content: '\e85e';
}
.e-input-group-icon.e-input-down:before { /* csslint allow: adjoining-classes */
content: "";
}
.e-input-group-icon.e-input-plus:before { /* csslint allow: adjoining-classes */
content: '\e7ba';
}
.e-input-group-icon.e-input-minus:before { /* csslint allow: adjoining-classes */
content: '\e814';
}
.e-input-group-icon.e-input-date:before { /* csslint allow: adjoining-classes */
content: "";
}
.e-input-group-icon.e-input-left:before { /* csslint allow: adjoining-classes */
content: '\e904';
}
.e-input-group-icon.e-input-right:before { /* csslint allow: adjoining-classes */
content: '\e913';
}
.e-input-group-icon.e-input-reload:before { /* csslint allow: adjoining-classes */
content: '\e837';
}
.e-input-group-icon.e-input-search:before { /* csslint allow: adjoining-classes */
content: '\e806';
}
#input-container .e-input-group { /* csslint allow: adjoining-classes */
margin: 30px 0;
}
#input-container .e-float-input { /* csslint allow: adjoining-classes */
margin: 30px 0;
}
To place the icon on left side of the TextBox, create a div element with the class
e-input-in-wrap
as wrapper to the input element and place thefloating line
,floating text
, and right side icon element within it. Add thee-float-icon-left
class to the TextBox container element.
With clear button and floating label
The clear button is added to the input for clearing the value given in the TextBox. It is shown only when the input field has a value, otherwise not shown.
You can add the clear button to the TextBox by enabling the showClearButton API in textbox
var inputobj = new ej.inputs.TextBox({
placeholder: 'First Name',
floatLabelType: 'Never'
});
inputobj.appendTo('#firstName');
var inputobj1 = new ej.inputs.TextBox({
placeholder: 'Last Name',
floatLabelType: 'Auto'
});
inputobj.appendTo('#lastName');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 TextBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 TextBox Components" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div class='wrap'>
<div id="input-container" class="textboxes">
<h4> Textbox with clear icon</h4>
<input id="firstName"/>
</div>
<div id="input-container" class="textboxes">
<h4>Floating Textbox with clear icon</h4>
<input id="lastName"/>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.wrap {
box-sizing: border-box;
margin: 0 auto;
padding: 30px 10px;
width: 260px;
}
.textboxes {
margin-top: 15px;
}
Floating label without required attribute
You can render the Floating label TextBox
without required
attribute by manually float the label above of the TextBox using input events.
You can manually float the label above of the TextBox by adding the below list of classes to the floating label element. The classes are:
Class Name | Description |
---|---|
e-label-top | Floats the label above of the TextBox. |
e-label-bottom | Label to be placed as placeholder for the TextBox. |
ej.base.enableRipple(true);
/* To get the Input element */
var inputElement = document.getElementById('inpt1');
/* Update the label position based on Input value */
updateLabelState(inputElement.value, inputElement.parentElement.querySelector('.e-float-text'));
inputElement.addEventListener("focus", function () {
var label = this.parentElement.querySelector('.e-float-text');
label.classList.add('e-label-bottom');
label.classList.remove('e-label-top');
});
inputElement.addEventListener("blur", function () {
updateLabelState(this.value, this.parentElement.querySelector('.e-float-text'));
});
inputElement.addEventListener("input", function () {
updateLabelState(this.value, this.parentElement.querySelector('.e-float-text'));
});
/* Update the label position based on Input value */
/* e-label-top - Floats the label above of the TextBox */
/* e-label-bottom - Label to be placed as placeholder for the TextBox */
function updateLabelState(value,label) {
if (value) {
label.classList.add('e-label-top');
label.classList.remove('e-label-bottom');
} else {
label.classList.add('e-label-bottom');
label.classList.remove('e-label-top');
}
}
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 TextBox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 TextBox Components">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div class="wrap">
<div id="input-container">
<h4> Floating label without required attribute </h4>
<div class="e-float-input">
<input type="text" id="inpt1">
<span class="e-float-line"></span>
<label class="e-float-text"> Enter Value </label>
</div>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.wrap {
box-sizing: border-box;
margin: 0 auto;
padding: 20px 10px;
width: 260px;
}
Multi-line input with floating label
Add the HTML textarea element with the e-input
class to create default multi-line input.
Add the floating label support to the multi-line input
by creating the floating label structure as defined in the initial section.
ej.base.enableRipple(true);
// To get the all input fields and its container.
var inputElement = document.querySelectorAll('.e-input-group .e-input,.e-float-input.e-input-group input');
// Add 'e-input-focus' class to the input for achive ripple effect when focus on the input field.
for (var i = 0; i < inputElement.length; i++) {
inputElement[i].addEventListener('focus', function () {
var parentElement = this.parentNode;
if (parentElement.classList.contains('e-input-in-wrap')) {
parentElement.parentNode.classList.add('e-input-focus');
} else {
this.parentNode.classList.add('e-input-focus');
}
});
inputElement[i].addEventListener('blur', function () {
var parentElement = this.parentNode;
if (parentElement.classList.contains('e-input-in-wrap')) {
parentElement.parentNode.classList.remove('e-input-focus');
} else {
this.parentNode.classList.remove('e-input-focus');
}
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 TextBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 TextBox Components" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div class='wrap'>
<div id="input-container">
<textarea class="e-input"> Address </textarea>
<div class="e-float-input">
<textarea required></textarea>
<span class="e-float-line"></span>
<label class="e-float-text"> Address </label>
</div>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.wrap {
box-sizing: border-box;
margin: 0 auto;
padding: 20px 10px;
width: 260px;
}
#input-container .e-float-input { /* csslint allow: adjoining-classes */
margin: 30px 0;
}