const inputs = [...document.querySelectorAll("input[class='serv']")];
const res = document.getElementById("resultado");
const total = res.querySelector('[name="total"]');
const nomes = res.querySelector('[name="nomes"]');
inputs.forEach(x => x.addEventListener("change", () => {
total.value = inputs.filter(el => el.checked).reduce((sum, el) => sum + Number(el.value), 0);
nomes.value = inputs.filter(el => el.checked).map(el => el.name).join(',');
}));
const inputsp = [...document.querySelectorAll("input[class='prod']")];
const res = document.getElementById("resultadop");
const totalp = res.querySelector('[name="totalp"]');
const nomesp = res.querySelector('[name="nomesp"]');
inputsp.forEach(x => x.addEventListener("change", () => {
totalp.value = inputsp.filter(el => el.checked).reduce((sum, el) => sum + Number(el.value), 0);
nomesp.value = inputsp.filter(el => el.checked).map(el => el.name).join(',');
}));
<div id="resultado">
<input type="text" name="total" value="">
<input type="hidden" name="nomes" value="">
</div>
<input type=checkbox class='serv' name="serviço 1" value="1">
<input type=checkbox class='serv' name="serviço 2" value="2">
<input type=checkbox class='serv' name="serviço 3" value="3">
<div id="resultadop">
<input type="text" name="totalp" value="">
<input type="hidden" name="nomesp" value="">
</div>
<input type=checkbox class='prod' name="Produto 1" value="1">
<input type=checkbox class='prod' name="Produto 2" value="2">
<input type=checkbox class='prod' name="Produto 3" value="3">
I came across an issue, I'm trying to use the same code on the same page making the changes to products. does anyone know why it is giving conflict even if I try to make the changes?