let fieldId = 0; for(let i = 0; i < 64; i++){ fieldId = i.toString(); document.getElementById("field"+fieldId).selected = 0; } let somethingIsSelected = false; function selectField(field){ console.log(field.id); for(let i = 0; i < 64; i++){ fieldId = i.toString(); if(document.getElementById("field"+fieldId).selected === 1){ somethingIsSelected = true; } } if(!somethingIsSelected) { field.style.backgroundColor = "rgb(227,8,8, 0.3)"; field.selected = 1; } else if (field.selected === 1){ field.style.backgroundColor = "rgb(255,255,255, 1)"; field.selected = 0; somethingIsSelected = false; } }