`.
### Imperative operations
If you need to imperatively perform an operation, you have to obtain a [reference to the DOM node](/react/docs/more-about-refs.html#the-ref-callback-attribute).
For instance, if you want to imperatively submit a form, one approach would be to attach a `ref` to the `form` element and manually call `form.submit()`.
## Examples
### Basic Text Input
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {value: ""};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert("Text field value is: '" + this.state.value + "'");
}
render() {
return (
Submit
);
}
}
ReactDOM.render(, document.getElementById('root'));
```
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/JRmZjz?editors=0010)
### Basic Textarea
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {value: ""};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert("Textarea value is: '" + this.state.value + "'");
}
render() {
return (
Submit
);
}
}
ReactDOM.render(, document.getElementById('root'));
```
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/kkAQPp?editors=0010)
### Basic Select
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {value: "B"};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert("Select value is: '" + this.state.value + "'");
}
render() {
return (
Apple
Banana
Cranberry
Submit
);
}
}
ReactDOM.render(, document.getElementById('root'));
```
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/pExQbL?editors=0010)
### Basic Radio Button
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {value: "B"};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert("Radio button value is: '" + this.state.value + "'");
}
render() {
return (
Option A
Option B
Option C
Submit
);
}
}
ReactDOM.render(, document.getElementById('root'));
```
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/WGaYVg?editors=0010)
### Basic Checkbox
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {checked: ["B"]};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
let val = event.target.value;
let checked = this.state.checked.slice(); // copy
if(checked.includes(val)) {
checked.splice(checked.indexOf(val), 1);
} else {
checked.push(val);
}
this.setState({checked: checked})
}
handleSubmit(event) {
alert("Boxes checked are: '" + this.state.checked + "'");
}
render() {
return (
Option A
Option B
Option C
Submit
);
}
}
ReactDOM.render(, document.getElementById('root'));
```
[Try it on CodePen.](https://codepen.io/ericnakagawa/pen/kkAzPO?editors=0010)
### Form Events
Event names:
```
onChange onInput onSubmit
```