1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | //au changement de l'un des input, on execute la function checkTotal()
document.querySelectorAll('input[type="number"]').forEach(input => {
	input.addEventListener('change', checkTotal);
});
 
function checkTotal() {
//on prend tous les inputs, et on applique une function de reduce() qui permet de reduire un tableau (ici les inputs) en une seule valeure numerique de sortie
	const total = [...document.querySelectorAll('input[type="number"]')].reduce((acc,input) => {
    return acc + parseInt(input.value);
  }, 0);
	if(total < 1) {
  document.querySelector('input[type="submit"]').setAttribute('disabled','');
  } else {
  document.querySelector('input[type="submit"]').removeAttribute('disabled');
  }
}
// on execute la verification (vu que par default, on bouton est actif
checkTotal(); | 
Partager