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 :: install express generator 
Javascript :: sort array of objects in ascending order in js 
Javascript :: add event listener to all a tags 
Javascript :: javascript check if array is empty or null or undefined 
Javascript :: socket.id 
Javascript :: assign random colors react chartjs 
Javascript :: does pycharm support javascript 
Javascript :: resize canvas javascript 
Javascript :: download datepicker js 
Javascript :: add an object to index 0 array js 
Javascript :: javascript array multidimensional push 
Javascript :: cloudinary react 
Javascript :: javascript insertbefore 
Javascript :: this.setstate prevstate 
Javascript :: javascript list class properties 
Javascript :: using python with javascript 
Javascript :: Icons library in react 
Javascript :: regex match any character 
Javascript :: async map js 
Javascript :: smtp js 
Javascript :: react js docker 
Javascript :: Simple interest in javascript 
Javascript :: adding a if stement in jsx 
Javascript :: json comment 
Javascript :: overflowx javascript 
Javascript :: react recursive component 
Javascript :: how to check empty object js 
Javascript :: swap scroll right in react native 
Javascript :: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. 
Javascript :: return inside for loop javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =