/* Validar checkboxes clickeados */
var checkboxes = document.querySelectorAll('.single-checkbox');
var clickeados = new Array();
checkboxes.forEach(elem => {
elem.addEventListener('change', (event) => {
event.stopPropagation();
if(elem.checked) {
if(!clickeados.includes(elem)){
clickeados.push(elem);
// console.log(clickeados);
}
}
else{
if(clickeados.includes(elem)){
clickeados.pop(elem);
// console.log(clickeados);
}
}
});
});
/* Validar cuantos gustos fueron seleccionados antes de hacer el submit */
$('#vasitoForm').on('submit', (event) => {
if(clickeados.length == 0){
event.preventDefault();
document.getElementById("errorGustos").innerHTML = "Debe seleccionar por lo menos un gusto";
}
else if(clickeados.length > 0 && clickeados.length < 3){
console.log(clickeados);
var myArray = JSON.stringify(clickeados);
// $('#vasitoForm').attr('action', myArray + '/pedido');
console.log(myArray);
}
});
The console.log(clickeados) prints me the array of input's perfectly, but when I want to convert it to JSON and print it again the JSON comes out empty...
Array [ input.single-checkbox ] home.js:59:5
[{}] home.js:62:9
When
JSON.stringify
it iterates your array to convert it to a string, it comes across the elementinput.single-checkbox
which is an HTML node and it doesn't know how to convert it to a string, so it's left as an empty object.I recommend that you instead build your own object with only the properties you use from the HTML node and that is what you convert via
JSON.stringify