Tuesday, January 31, 2023

How to get input field value on button click in reactjs?

Get input field value on button click in reactjs


Hello, welcome to In this post, I will tell you, How to get input field value on button click in reactjs? Reactjs is a Javascript Library to build user interface.

In this post, I made simple form and input field and input button. On button click, I am getting the input box value and this is tricky and in future post, I will do more this form.

Here is the working code and you need to add this into your index.js file:
import React from 'react';
import ReactDOM from 'react-dom';

class Hello extends React.Component{
      this.state = {value: ''};
      this.addValue = this.addValue.bind(this);
      this.updateInput = this.updateInput.bind(this);
      if(this.state.value !=undefined)
        alert('Your input value is: ' + this.state.value)
      return (
      <form onSubmit={this.addValue}>
      <input type="text" onChange={this.updateInput} /><br/><br/>
      <input type="submit" value="Click Me :)"/>

  ReactDOM.render(<Hello />, document.getElementById('root'));

 I will do this post in parts and I can say this is the first part and In future posts, I will do more with reactjs. If you have any query related to this post then please comment below.

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 8, Angular 9, Angular 10, Angular 11, Angular12, Angular 13, Angular 14, Angular 15, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.