I have some inputs in my HTML like:
<input type="text" name="offer-add[lname][]" class="form-control offer-input" id="offer-add-lname" placeholder="">
<input type="text" name="offer-add[fname][]" class="form-control offer-input" id="offer-add-fname" placeholder="">
I want get all the values in JS as an array, but as array key I want e.g. lname, fname.
I tried with:
var values = $("input[name^='offer-add']")
.map(function(){return $(this).val();}).get();
so got as key only numbers (0, 1, 2, …).
2
Answers
If you want the input’s value and part of id as an object on a button click then try the following way:
Get the inputs, extract the name and value, and then extract the necessary part of the name using a regex. Finally, convert the key/value pairs into an object.