Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to pass dynamicallu created textbox value in ReactJS

An Introduction to Dynamically Created Textbox Values

Let's imagine you are building a form in ReactJS. You want to allow users to add as many inputs as they want. This can be achieved with dynamically created textboxes. But how do you capture the value typed into these textboxes?

Understanding the Problem

Visualize a scenario where you are creating a survey form. The user should be able to add as many questions as they want, and you should be able to capture the text of those questions.

In simple terms, we are allowing users to create their own textboxes and we want to know what they type in those textboxes.

Creating a Dynamic Textbox

To create a dynamic textbox, we first need a way to add new boxes. A simple approach is to use a button that creates a new textbox each time it's clicked. For this, we would need to use a state in our component to keep track of the number of textboxes.

Here's an example:

import React, { useState } from 'react';

function Form() {
  const [count, setCount] = useState(0);

  const addTextbox = () => {
   setCount(count + 1);
  };

  return (
    <div>
      {[...Array(count)].map((x, i) =>
        <input key={i} type="text" />
      )}
      <button onClick={addTextbox}>Add Textbox</button>
    </div>
  );
}

export default Form;

In this code, we start with a count of zero and add one each time the button is clicked, creating a new textbox.

Capturing Textbox Values

Now, how do we capture the values of these textboxes? We can use an array to save the values of each textbox. This array will be part of the state of our component, just like the count variable.

Let's see how to do this:

import React, { useState } from 'react';

function Form() {
  const [count, setCount] = useState(0);
  const [values, setValues] = useState([]);

  const addTextbox = () => {
   setCount(count + 1);
  };

  const handleInputChange = (i, event) => {
    values[i] = event.target.value;
    setValues([...values]);
  }

  return (
    <div>
      {[...Array(count)].map((x, i) =>
        <input key={i} type="text" onChange={event => handleInputChange(i, event)} />
      )}
      <button onClick={addTextbox}>Add Textbox</button>
    </div>
  );
}

export default Form;

In this code, we added a new function handleInputChange(). This function is called whenever the text in any textbox changes (onChange). It captures the new text (with event.target.value) and updates the corresponding element in the values array.

Testing Your Code

To verify if the values are correctly being captured, we can add a button that logs the current state of the values array. Here's how:

import React, { useState } from 'react';

function Form() {
  // ... other code

  const showValues = () => {
    console.log(values);
  }

  return (
    <div>
      {/* ... other code */}
      <button onClick={showValues}>Show Values</button>
    </div>
  );
}

export default Form;

By clicking the "Show Values" button, you will be able to see the current state of your values array in the console.

Wrapping Up

In a nutshell, we have learned how to create dynamic textboxes in ReactJS and capture their values. This is like giving your users a piece of paper and a pen, and letting them write down as many questions as they want. When they are done, you collect the paper and read what they wrote.

However, in our case, the paper is the ReactJS component, the pen is the textboxes, and the reading is done by our values state.

This is a basic application of dynamic textbox values. As you advance in your programming journey, you'll find more complex and exciting ways to use this concept. Happy coding!