`.
### 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
### Controlled Input
```javascript{10,23,24}
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 it out on CodePen.](https://codepen.io/gaearon/pen/JRVaYB?editors=0010)
### Controlled Textarea
```javascript{10,22,23}
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 it out on CodePen.](https://codepen.io/gaearon/pen/NRmLxN?editors=0010)
### Controlled Select
```javascript{10,20}
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 it out on CodePen.](https://codepen.io/gaearon/pen/qawrbr?editors=0010)
### Uncontrolled Radio Button
```javascript{25,34,35,44}
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 it out on CodePen.](https://codepen.io/gaearon/pen/ozOPLJ?editors=0010)
### Uncontrolled Checkbox
```javascript{37,45,46,54}
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) {
const value = event.target.value;
// Copy the object so we don't mutate the old state.
// (This requires an Object.assign polyfill):
const checked = Object.assign({}, this.state.checked)
if (!checked[value]) {
checked[value] = true;
} else {
checked[value] = false;
};
this.setState({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/gaearon/pen/rrbkWz?editors=0010)