Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get react form input data, How get form input data in react

import { useForm } from "react-hook-form";

export default const SimpleForm = () => {
  
  // For Multiple input
  const { register, handleSubmit, reset } = useForm();

  const onSubmit = (data) => {
      const newData = new FormData();
      newData.append("userid", currentUser._id);
      newData.append("username", currentUser.name);
      newData.append("comments", data.comments);
      // In Console get Json Formatedata You can fatch 
      // Post To the database to save 
      console.log([...newData]); 
    
      // Reset the Form 
      reset();
  }

  return (
      <form onSubmit={handleSubmit(onSubmit)} className="form">
          <input type="text" {...register("comments")} />
      </form>
  )


}



 // 2nd Options 
<form onSubmit={handleSubmit(onSubmit)} className="form">
	<input type="text" name="username"/>
</form>
                
const onSubmit = (event) => {
  event.preventDefault();
  console.log(event.target.elements.username.value) // from elements property
  console.log(event.target.username.value)          // or directly
}

Comment

how to get data from input field in react js

also use usestate then connect useState to input box by using setName()

<input placeholder="example Mike" 
onChange={(event) => {setName(event.target.value);}} />
Comment

PREVIOUS NEXT
Code Example
Javascript :: get selected text input javascript 
Javascript :: javascript string proper case 
Javascript :: iife in javascript 
Javascript :: Disable/remove pagination from react material-table 
Javascript :: how to enable emit on react in vs code 
Javascript :: classic asp get json from file 
Javascript :: nodejs delete object key 
Javascript :: javascript array slice 
Javascript :: javascript como recorrer un array multidimensional 
Javascript :: vue js datetime convert 
Javascript :: javascript fill 2 dimensional array 
Javascript :: ternary operator in button react 
Javascript :: how to increment variable value in javascript 
Javascript :: axios in functional component 
Javascript :: prev props 
Javascript :: selector for redux 
Javascript :: javascript how to get rid of e with number input 
Javascript :: react export 
Javascript :: Get the url and parse or url.parse deprecated solved 
Javascript :: get file extension file upload control in javascript 
Javascript :: command to run nextjs project 
Javascript :: next-dev.js?3515:32 Warning: Prop `className` did not match. Server Client 
Javascript :: how to make page scroll to the top jsx 
Javascript :: get user input node js console 
Javascript :: image onclick react 
Javascript :: js replace diacritics 
Javascript :: limit data with axios in react js 
Javascript :: react render twice v18 
Javascript :: open youtube video at specific time javascript 
Javascript :: javascript dataset 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =