`.
### 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 Controlled 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 Controlled 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 Uncontrolled 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 Uncontrolled Checkbox
```javascript
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {checked: {'A': false, 'B': true, 'C': false}};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
let value = event.target.value;
let checked = this.state.checked; // copy
if (!checked[value]) { checked[value] = true; } else { checked[value] = false; }
this.setState({checked: checked})
}
handleSubmit(event) {
alert('Boxes checked: ' +
(this.state.checked.A ? 'A ' : '') +
(this.state.checked.B ? 'B ' : '') +
(this.state.checked.C ? 'C' : '')
);
}
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
```