Author -  Sai gowtham

Get the input value on button click in React

In this tutorial, we are going to learn how to get the input value on button click in react with the help of examples.

Consider we have a component like this.

import React from "react";

function App() {
    return (
      <div>        <input placeholder="Enter name" />
        <button>Log value</button>
      </div>
    );
}
export default App;

Now, we need to get the above input field value by clicking on the button.

To get a input value on button click, define a state variable that tracks the input value and add a onChange event handler to it. Inside the event handler method update the state variable with the input value.

At last add a click event handler to the button and access the input value from the react state variabale.

Here is an example:

import React,{useState} from "react";

function App(){

const [name, setName] = useState("");

  const handleInputChange = (event) => {
    setName({ name: event.target.value });
  };

   handleBtnClick = () => {
    console.log(this.state.name);
  };

    return (
      <div>
        <input  value={name} onChange={this.handleInput}/>
        <button onClick={handleBtnClick}>Log value</button>
      </div>
    );
}

In the above code, we are storing the input field value inside the name property. So, whenever the input value is changed it updates the name property using the setName({ name: event.target.value });.

If we click on a button it logs the input value from the react state.

Get input value on button click using react refs

Similarly, we can use the useRef hook in react to get the input field value on buton click.

Example:

import React, { useRef } from "react";

function App() {
  const inputRef = useRef(null);

  handleBtnClick = () => {
    console.log(nameRef.current.value);
  };

  return (
    <div>
      <input ref={inputRef} placeholder="Enter name"/>
      <button onClick={logValue}>Log value</button>
    </div>
  );
}

In the above code, we first initialized the useRef() hook with a value null and passed it to the input element using the ref={inputRef}. So it returns a input element dom node just like document.getElementByid().

Now, we can access the input value using the nameRef.current.value.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY