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 :: change form action js 
Javascript :: select first 3 letters js 
Javascript :: while loop countdown javascript 
Javascript :: get the id of a div in jquery 
Javascript :: javascript create image 
Javascript :: nodejs express api 
Javascript :: regex for entire word 
Javascript :: javascript download string as file 
Javascript :: js on dom content loaded 
Javascript :: workbox push notifications 
Javascript :: datatable after loading function 
Javascript :: Set Custom User Agent react 
Javascript :: Express’s default X-Powered-By header Disabling 
Javascript :: flutter text with icon 
Javascript :: add color to console 
Javascript :: localstorage save array 
Javascript :: graphql float 
Javascript :: js not equal to null 
Javascript :: js sleep 1 second 
Javascript :: how to get relative postiion mouse click on element 
Javascript :: javascript substring after character 
Javascript :: how To clear all the input element inside div using jquery 
Javascript :: useffect compare previous value to current 
Javascript :: validate age javascript 
Javascript :: vue router 404 page 
Javascript :: remove time from date javascript 
Javascript :: eslint ignore file rule 
Javascript :: iterate over map key value javascript 
Javascript :: a <route is only ever to be used as the child of <routes element" 
Javascript :: How to make the width of a react native element adjust according to the contents 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =