Groups in EJ2 TypeScript Textbox control

4 Jan 202519 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>
      <!--element which is going to render the TextBox-->
      <input id="firstName"/>
    </div>
```
    import { TextBox } from '@syncfusion/ej2-inputs'

    let inputobj: TextBox = new TextBox({
        placeholder: 'Enter Date',
    });

inputobj.appendTo('#firstName');
```

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>
      <!--element which is going to render the TextBox-->
      <input id="firstName"/>
    </div>
```
    import { TextBox } from '@syncfusion/ej2-inputs'

    let inputobj: TextBox = new TextBox({
        placeholder: 'Enter the name',
        floatLabelType: 'Auto'
    });

inputobj.appendTo('#firstName');
```

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.

import { TextBox } from '@syncfusion/ej2-inputs';

// First TextBox without an icon
let textIconAppend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  created: () => {
    textIconAppend.addIcon('append', 'e-date-icon');
  },
});
textIconAppend.appendTo('#textIconAppend');

// Second TextBox with an icon
let textIconPrepend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  created: () => {
    textIconPrepend.addIcon('prepend', 'e-date-icon');
  },
});
textIconPrepend.appendTo('#textIconPrepend');

let textIconAppendPrepend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  created: () => {
    textIconAppendPrepend.addIcon('prepend', 'e-date-icon');
    textIconAppendPrepend.addIcon('append', 'e-chevron-down-fill');
  },
});
textIconAppendPrepend.appendTo('#textIconAppendPrepend');

// First TextBox without an icon
let textFloatIconAppend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  floatLabelType: 'Auto',
  created: () => {
    textFloatIconAppend.addIcon('append', 'e-date-icon');
  },
});
textFloatIconAppend.appendTo('#textFloatIconAppend');

// Second TextBox with an icon
let textFloatIconPrepend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  floatLabelType: 'Auto',
  created: () => {
    textFloatIconPrepend.addIcon('prepend', 'e-date-icon');
  },
});
textFloatIconPrepend.appendTo('#textFloatIconPrepend');

let textFloatIconAppendPrepend: TextBox = new TextBox({
  placeholder: 'Enter Date',
  floatLabelType: 'Auto',
  created: () => {
    textFloatIconAppendPrepend.addIcon('prepend', 'e-date-icon');
    textFloatIconAppendPrepend.addIcon('append', 'e-chevron-down-fill');
  },
});
textFloatIconAppendPrepend.appendTo('#textFloatIconAppendPrepend');
<!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>
              <input id="textIconAppend" />
            </div>
    
            <div>
              <input id="textIconPrepend" />
            </div>
    
            <div>
              <input id="textIconAppendPrepend" />
            </div>
    
            <h4>Floating label with icons</h4>
    
            <div>
              <input id="textFloatIconAppend" />
            </div>
    
            <div>
              <input id="textFloatIconPrepend" />
            </div>
    
            <div>
              <input id="textFloatIconAppendPrepend" />
            </div>



          </div>
        </div>
    </div>
</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-chevron-down-fill:before {
  content: '\e70d';
}

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 the floating line, floating text, and right side icon element within it. Add the e-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

import { TextBox } from '@syncfusion/ej2-inputs'

let inputobj: TextBox = new TextBox({
    placeholder: 'First Name',
    floatLabelType: 'Never'
});

inputobj.appendTo('#firstName');

let inputobj1: TextBox = new 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>
</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.
import {TextBox } from '@syncfusion/ej2-inputs';

let inputObj: TextBox = new TextBox({
    placeholder: 'Enter the name',
    floatLabelType: 'Auto',
  });
  inputObj.appendTo('#input1');
<!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/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/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> Floating label without required attribute </h4>
            <div >
                <input id='input1' />
            </div>
          </div>
        </div>
    </div>
</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.

import { TextBox } from '@syncfusion/ej2-inputs';


let textBoxObj: TextBox = new TextBox({
  placeholder: 'Enter address',
  multiline: true,
});
textBoxObj.appendTo('#input1');
<!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'>
            <input id="input1"/>
        </div>
    </div>>
</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;
}

See Also