I have initialized a state object to store the dynamically added input elements by a user via a button they click. In this input element, a user can type a number. I’m trying to get the number value and value of every element that is dynamically added but I am not sure how to exactly do that.
My code:
const [addedExpenses, setAddedExpenses] = useState([]);
function addExpense() {
const userNewExpense = (
<div class ="new-expense">
<input id="written-expense"></input>
<input id="money-expense" ></input>
</div>
)
setAddedExpenses([...addedExpenses, userNewExpense])
3
Answers
you can try this one.
Hope this satisfies your question. React-StackBlitz . StackOverflow already has a similar question try exploring more before posting here.
You can do it like this :
Or you can see the live JS :
Live sample