Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React Form submit

class NameForm extends React.Component {
  handleSubmit = (event) => {
    event.preventDefault()
    console.log(event.target[0].value)
    console.log(event.target.elements.username.value)
    console.log(event.target.username.value)
    console.log(this.inputNode.value)
  }
  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input
            type="text"
            name="username"
            ref={node => (this.inputNode = node)}
          />
        </label>
        <button type="submit">Submit</button>
      </form>
    )
  }
}
Comment

Submit form react js

class MyForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '',
      submit: ''
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleChange(event) {
    this.setState({
      input: event.target.value
    });
  }
  handleSubmit(event) {
    event.preventDefault()
    this.setState({
      submit: this.state.input
    });
    
  }
  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
            <input value={this.state.input} onChange={this.handleChange}/>
          <button type='submit'>Submit!</button>
        </form>
        
          <h1>{this.state.submit}</h1>
       
      </div>
    );
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript check if variable is object 
Javascript :: react native run ios release 
Javascript :: regex phone number 
Javascript :: convert date to readable format javascript 
Javascript :: get url react 
Javascript :: js array to csv 
Javascript :: electron mass to amu 
Javascript :: replace space with hyphen/dash javascript 
Javascript :: react js image path src from local folder 
Javascript :: window.print a div 
Javascript :: iso to date javascript 
Javascript :: angular set content type 
Javascript :: string-mask javascript 
Javascript :: javascript get element by multiple class names 
Javascript :: check if var is NaN 
Javascript :: Nodemailer gmail new configuration 
Javascript :: n javascript 
Javascript :: sh: /Users/ahmedqadri/Desktop/Projects/stockNotesAPP-frontend/node_modules/.bin/react-scripts: Permission denied 
Javascript :: javascript get class name 
Javascript :: javascript remove style 
Javascript :: js select by data attribute 
Javascript :: jquery click not working on dynamic content 
Javascript :: javascript toPrecision() Method 
Javascript :: findone sequelize 
Javascript :: prevent blur event on click 
Javascript :: jquery onload function for div 
Javascript :: bootstrap 4 navbar-collapse not working angular 
Javascript :: js date enlever jour 
Javascript :: statement and expression in js 
Javascript :: wordpress jquery slide out navigation 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =