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 :: on button click show collapse div jquery 
Javascript :: nodejs add element to array 
Javascript :: js reduce method 
Javascript :: yup validation based on another field value 
Javascript :: jquery find and replace text 
Javascript :: js get datatable attr value on click 
Javascript :: react cors error 
Javascript :: Icons library in react 
Javascript :: json schema validator allows null 
Javascript :: string theory 
Javascript :: JQuery datatable with ajax, post API call hook 
Javascript :: javascript string repeat 
Javascript :: Javascript object convert into JSON 
Javascript :: how to concatenate a string in javascript 
Javascript :: vue component lifecycle 
Javascript :: how to return argument in javascript 
Javascript :: css react 
Javascript :: New JSDOM and querySelector elems textContent 
Javascript :: jquery vertical scroll 
Javascript :: what are closures 
Javascript :: how make calender in bootstrap 
Javascript :: js remove escape characters from json 
Javascript :: jquery works until modal is shown 
Javascript :: multi dimensional array javascript 
Javascript :: how to get variable from url in javascript 
Javascript :: chrome.runtime.sendMessage 
Javascript :: mariadb JSON_ARRAYAGG does not exist 
Javascript :: upload file to database with axios and formData 
Javascript :: array -1 javascript 
Javascript :: date to string format javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =