I would like to know if we can loop through event.target and get attribute values for each element inside a form. I am trying to implement this in Reactjs. There is just a basic form as given below
<form onSubmit={this.handleSubmit}> <input type="text" name="name" /> <input type="email" name="email" /> <input type="password" name="password" /> </form> and in the handleSubmit function, the code is as given below
handleSubmit(event) { event.preventDefault(); let target = event.target; let formData = {}; formData.username = target.name.value; formData.email = target.email.value; formData.passowrd = target.passowrd.value; console.log(formData); } I get the user filled data in the console.log as required. But as you can see I need specify each and every element to get the formData, Imagine a form with 20-30 elements.
I tried to do something as given below
count = 0; formData = []; foreach(target as item) { formData[count][item.name] = item.value; } It's basically PHP code But put here so that you can see what am trying to do. I tried to do this js and it gives me all kind of errors. Please let know if it's possible in js If so? How can I achieve it?