How to add an array to the last in this object:
const formElment = document.querySelector(".form");
formElment.addEventListener("submit", (event) => {
event.preventDefault();
var array = [];
var checkboxes = document.querySelectorAll("input[type=checkbox]:checked");
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].value);
console.log(checkboxes[i].id);
}
const formData = new FormData(formElment);
const data = Object.fromEntries(formData);
console.log(data);
});
I used eventlistner
for get form data and checkbox=checked. So now I want add those selected checkbox id to the end of the object and send through an API.
2
Answers
I think you can add the values of array to FormData.
formData.append("checkedValues", array.join(","));
And then you can post your form to your API.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
You can add the checkbox values after you have created
data
using FormData and Object.fromEntries.Notice that the checkbox elements should not have a name attribute — then they are not included in the formData object.