I have a form
like below
<form className="ui form" onSubmit={this.update}>
<input type="text" defaultValue={this.props.element} onChange={this.handleChange1}/>
<input type="button" value="Update" className="ui positive icon button"/>
</form>
My update function is like below
update = event => {
console.log('hello');
event.preventDefault();
}
But it is not working.
The submit button should not be of type button
, but type submit
.
Example
class App extends React.Component {
update = event => {
console.log("hello");
event.preventDefault();
};
render() {
return (
<form className="ui form" onSubmit={this.update}>
<input
type="text"
defaultValue={this.props.element}
onChange={this.handleChange1}
/>
<input
type="submit"
value="Update"
className="ui positive icon button"
/>
</form>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With