0

Given below. I'm trying to find a way how to append the input value under the same keys using FormData

results:

sku: [saafasf, hjhkk]
quantity: [3, 3]
<div>
 <input value="saafasf" readonly="" id="sku" name="sku"> 
 <input value="3" readonly="" id="quantity" name="quantity">
 <input value="hjhkk" readonly="" id="sku" name="sku"> 
 <input value="3" readonly="" id="quantity" name="quantity">
</div>

Trying to find a way how to get the array results with multiple values under one key via FromData

zalala
  • 51
  • 2

1 Answers1

0

Multiple HTML elements in the same DOM can not contain the same id. Hence, you have to convert those into class to get it work.

And then you can access input values under same class using document.querySelectorAll() method.

Live Demo :

const skuArr = Array.from(document.querySelectorAll(".sku"), ({ value }) => value);
const quantityArr = Array.from(document.querySelectorAll(".quantity"), ({ value }) => value);

const formDataObj = {
  sku: skuArr,
  quantity: quantityArr
};

console.log(formDataObj);
<div>
 <input value="saafasf" readonly="" class="sku" name="sku"> 
 <input value="3" readonly="" class="quantity" name="quantity">
 <input value="hjhkk" readonly="" class="sku" name="sku"> 
 <input value="3" readonly="" class="quantity" name="quantity">
</div>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123