When the checkbox is selected, it is supposed to make some elements null and turn to grey. The function was working a few days ago but suddenly is not, i don't really understand why. I used elements of it in another function, and I don't know if that's related but I'll leave the code for those too.
The problem is that, how it is, it returns "Cannot set property 'backgroundColor' of undefined" regarding, firstly, morada2, but that one doesn't return null. It enters the if, but it's not capable of executing what's inside. If I change the document.getElementByID.checked
to checkBox.checked
var checkcheck = document.getElementById("checkcheck").value;
var checkBox = document.getElementById("idtipomorada").value;
var morada2 = document.getElementById("morada2").value;
var cidade2 = document.getElementById("cidade2").value;
var codpostal2 = document.getElementById("codpostal2").value;
var concelho2 = document.getElementById("concelho2").value;
var distrito2 = document.getElementById("distrito2").value;
var pais2 = document.getElementById("pais2").value;
function validate() {
if (document.getElementById("idtipomorada").checked == true){
console.log("erro aqui");
morada2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("morada2").disabled = true;
cidade2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("cidade2").disabled = true;
codpostal2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("codpostal2").disabled = true;
concelho2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("concelho2").disabled = true;
distrito2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("distrito2").disabled = true;
pais2.style.backgroundColor = "rgb(150, 155, 153)";
document.getElementById("pais2").disabled = true;
checkcheck = "true";
} else if (checkBox.checked == false) {
morada2.style.backgroundColor = "white";
document.getElementById("morada2").disabled = false;
cidade2.style.backgroundColor = "white";
document.getElementById("cidade2").disabled = false;
codpostal2.style.backgroundColor = "white";
document.getElementById("codpostal2").disabled = false;
concelho2.style.backgroundColor = "white";
document.getElementById("concelho2").disabled = false;
distrito2.style.backgroundColor = "white";
document.getElementById("distrito2").disabled = false;
pais2.style.backgroundColor = "white";
document.getElementById("pais2").disabled = false;
checkcheck = "false";
} else {
console.log("erro");
}
return(checkcheck);
console.log(checkcheck);
}
function guardarMorada0() {
var checked = document.getElementById("checkcheck").value;
if (checked == "true") {
guardarUma();
} else {
guardarDuas();
}
}
<input type="checkbox" id="idtipomorada" onclick="validate()">
<label style="font-weight: lighter;"> A minha morada de faturação é a mesma que a morada de envio</label>
<p id="checkcheck"></p>
, it doesn't recignize either the if or the else, and so, doesn't return the backgroundColor error. I've tried putting the variables outside, inside, change names, change trues and falses to strings and back and nothing works. It's not a cache problem either. Note: the paragraph is there simply because I needed to verify is the return values were the ones I wanted.