Text Elements

Comprehensive examples of TextElement and its subclasses with autocomplete

Overview

Now.js provides powerful text input elements with various features:

📝 TextElement

Basic text input with autocomplete support

data-element="text"

📧 EmailElement

Email validation and formatting

data-element="email"

🔗 UrlElement

URL validation

data-element="url"

👤 UsernameElement

Username or email validation

data-element="username"

🔍 SearchElement

Search input with live results

data-element="search"

📱 MaskElement

Input masking for phone, date, ID card

data-element="mask"

🏷️ TagsElement

Multiple tags/chips input

data-element="tags"

🔤 TextareaElement

Multi-line text with auto-resize

data-element="textarea"

Demo 1: Static Autocomplete

Autocomplete from static datalist or inline options:

Static Autocomplete Examples

Select a country (uses <datalist>)
Source from global variable (PROGRAMMING_LANGUAGES)
Custom rendering with emoji icons
Limited to 5 results in dropdown

Code Example


            <!-- From Datalist -->
            <input type="text" name="country"
            data-element="text"
            data-autocomplete="true"
            list="country-list">
            <datalist id="country-list">
            <option value="TH" label="Thailand">Thailand</option>
            <option value="JP" label="Japan">Japan</option>
            </datalist>

            <!-- From Global Variable -->
            <input type="text" name="language"
            data-element="text"
            data-autocomplete="true"
            data-source="PROGRAMMING_LANGUAGES"
            data-min-length="1">
          

Demo 2: API Autocomplete

Load autocomplete options from API endpoint:

API-Based Autocomplete

Loads products from API on each keystroke
500ms delay for API rate limiting
Custom rendering with multiple fields (name, email, phone)

Code Example


            <!-- Simple API Autocomplete -->
            <input type="text" name="product"
            data-element="text"
            data-autocomplete="true"
            data-source="api/products.php"
            data-min-length="2"
            data-delay="300">

            <!-- With Custom Callback -->
            <input type="text" name="customer"
            data-element="text"
            data-autocomplete="true"
            data-source="api/customers.php"
            data-callback="renderCustomerItem">
          

API Response Format


            // Simple format (array of objects)
            {
            "success": true,
            "data": [
            {"value": "1", "text": "Product A"},
            {"value": "2", "text": "Product B"}
            ]
            }

            // Alternative formats (auto-detected)
            [{"id": "1", "name": "Product A"}] // id→value, name→text
            [{"key": "1", "label": "Product A"}] // key→value, label→text
            {"1": "Product A", "2": "Product B"} // Object format
          

Demo 3: Validated Input Types

Built-in validation for email, URL, and username:

Email, URL, Username

Auto lowercase, email validation
URL format validation
Accepts username_123 or email format
Must match email field

Demo 4: Mask Input

Auto-formatting with input masks:

Pre-defined Masks

Format: 99-9999-9999
Format: 9-9999-99999-99-9
Format: 9999-9999-9999-9999
Format: 999.999.999.999
Format: 99/99/9999
Format: 99:99

Custom Masks

Custom pattern: 99999
Custom pattern: AAA-9999 (letters + numbers)

Mask Pattern Characters


            9 - Any digit (0-9)
            a - Any letter (a-z, A-Z)
            A - Any letter (uppercase only)
            * - Any alphanumeric
            # - Any numeric with . , -
            X - Any character
          

Demo 5: Tags Input

Multiple value selection with tags/chips:

Tags Input Examples

Type and press Enter to add tag, Backspace to remove
Autocomplete suggestions from predefined list (JavaScript, HTML ...)
Maximum 5 tags allowed
Load categories from API endpoint
Autocomplete from datalist

Search input with live results and clear button:

Search Input Examples

Type at least 2 characters

Demo 7: Hierarchical Text (Address)

Reverse search across hierarchical data (Province → District → Subdistrict):

Thai Address (Auto-fill from any level)

Type province name to search
Type district name - auto-fills province
Type subdistrict - auto-fills district & province
Auto-filled from selection

Demo 8: Dynamic Options

Load autocomplete options dynamically via JavaScript:

Dynamic Options via JavaScript

Options loaded via TextElementFactory.populateFromOptions()

JavaScript Code


            // Load options dynamically
            const options = {
            dynamicItems: [
            {value: '1', text: 'Option 1'},
            {value: '2', text: 'Option 2'},
            {value: '3', text: 'Option 3'}
            ]
            };

            // Method 1: Populate specific element
            const input = document.getElementById('dynamic_field');
            TextElementFactory.populateFromOptions(input, options, 'dynamicItems');

            // Method 2: Populate all elements in container with data-options-key
            TextElementFactory.populateFromOptionsInContainer(form, options);

            // Method 3: Update options directly on instance
            const instance = ElementManager.getInstanceByElement(input);
            TextElementFactory.updateOptions(input, options.dynamicItems);
          

API Reference

Data Attributes

Attribute Type Description
data-element string Element type: text, email, url, username, search, mask, tags
data-autocomplete boolean Enable autocomplete: "true" or "false"
data-source string Autocomplete source: global variable name or API URL
data-min-length number Minimum characters before autocomplete (default: 2)
data-max-results number Maximum results in dropdown (default: 10)
data-delay number Debounce delay in ms (default: 300)
data-callback string Custom render callback function name
data-format string Mask format: tel, date, time, creditcard, ip, idcard, zipcode
data-pattern string Custom mask pattern
data-max-tags number Maximum tags allowed (TagsElement)
data-search-api string API URL for hierarchical search
data-search-field string Field name for hierarchical search