Angular TextBox - Handle the Value Change Event
When a user types a value into the TextBox, this value applies when the change event is raised. If you need the value to be applied on another event, set the valueChangeEvent property.
jQuery
$(function() { $("#textBoxContainer").dxTextBox({ valueChangeEvent: "keyup" }); });
Angular
<dx-text-box valueChangeEvent="keyup"> </dx-text-box>
import { DxTextBoxModule } from "devextreme-angular"; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxTextBoxModule ], // ... })
Vue
<template> <DxTextBox value-change-event="keyup"/> </template> <script> import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import { DxTextBox } from 'devextreme-vue/text-box'; export default { components: { DxTextBox } } </script>
React
import React from 'react'; import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import { TextBox } from 'devextreme-react/text-box'; class App extends React.Component { render() { return ( <TextBox valueChangeEvent="keyup" /> ); } } export default App;
To process a new TextBox value, you need to handle the value change event. If the handling function is not going to be changed during the lifetime of the UI component, assign it to the onValueChanged property when you configure the UI component.
jQuery
$(function() { $("#textBoxContainer").dxTextBox({ onValueChanged: function (e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } }); });
Angular
<dx-text-box [(value)]="textBoxValue" (onValueChanged)="handleValueChange($event)"> </dx-text-box>
import { DxTextBoxModule } from "devextreme-angular"; // ... export class AppComponent { textBoxValue = "The TextBox value"; handleValueChange(e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } } @NgModule({ imports: [ // ... DxTextBoxModule ], // ... })
Vue
<template> <DxTextBox :value.sync="textBoxValue" @value-changed="handleValueChange" /> </template> <script> import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import { DxTextBox } from 'devextreme-vue/text-box'; export default { components: { DxTextBox }, data() { return { textBoxValue: "The TextBox value" }; }, methods: { handleValueChange(e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } } } </script>
React
import React from 'react'; import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import { TextBox } from 'devextreme-react/text-box'; class App extends React.Component { constructor(props) { super(props); this.state = { textBoxValue: "The TextBox value" }; this.handleValueChange = this.handleValueChange.bind(this); } handleValueChange(e) { const previousValue = e.previousValue; const newValue = e.value; this.setState({ textBoxValue: newValue }); } render() { return ( <TextBox value={this.state.textBoxValue} onValueChanged={this.handleValueChange} /> ); } } export default App;
If you are going to change event handlers at runtime, or if you need to attach several handlers to the value change event, subscribe to this event using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
const valueChangedHandler1 = function (e) { const previousValue = e.previousValue; const newValue = e.value; // First handler of the "valueChanged" event }; const valueChangedHandler2 = function (e) { const previousValue = e.previousValue; const newValue = e.value; // Second handler of the "valueChanged" event }; $("#textBoxContainer").dxTextBox("instance") .on("valueChanged", valueChangedHandler1) .on("valueChanged", valueChangedHandler2);
See Also
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- TextBox - Handle the Keyboard Events
- TextBox Demos
- TextBox API Reference
If you have technical questions, please create a support ticket in the DevExpress Support Center.