1
Let’s cut to the chase, I thank you in advance.
const inputs = [...document.querySelectorAll("input[name='nomes[]']")];
const res = document.getElementById("resultado");
inputs.forEach(x => x.addEventListener("change", ()=> {
//esta função corre quando houve alteração de um dos checkboxes
res.innerHTML = ""; //limpar o resultado
//passar em todos os inputs e adicionar o seu value e <br> se tiver checked
inputs.forEach (y => res.innerHTML += y.checked ? y.value + "<br>" : "");
}));
<input type="checkbox" name="nomes[]" value="01" />
<input type="checkbox" name="nomes[]" value="02" />
<input type="checkbox" name="nomes[]" value="03" />
<input type="checkbox" name="nomes[]" value="04" />
<div id="resultado">
<input type="hidden" name="total" value="seria a soma dos valores">
<input type="hidden" name="nomes" value="seria os nomes[] marcados(A,B,C..)">
</div>
the code is working as it should, but would like to know how to make some change so that the input’s within the DIV receive the data instead of the DIV.
What name do you want to show? They all have the same name... :/
– Sergio
the names are being brought from BD, but to illustrate it would be "Keyboard, Mouse, Phone, Monitor"
– Ronaldo de Lima