I venture into a solution, which consists of the following:
Explanation
The tag <style>
is used solely for the declaration styles (CSS) for HTML. And it certainly should not be dynamically inserted in the page to change any style as you are tempted (though it is possible).
Having this knowledge, let's go into the correct mode. That would be to use the javascript itself to manipulate DOM HTML . Where you can manipulate style
of an element and consequently its properties, being in its case the property backgroundColor
, obtaining a result equivalent to that declared in the tag <style>
.
The implementation
What you can do then is the following:
if(senha.value != confirmar.value){
senha.style.backgroundColor = "#FF0000";
confirmar.style.backgroundColor = "#FF0000";
}else{
senha.style.backgroundColor = "#FFFFFF";
confirmar.style.backgroundColor = "#FFFFFF";
}
Being a complete example like this:
function verificarIgual() {
if (senha.value != confirmar.value) {
senha.style.backgroundColor = "#FF0000";
confirmar.style.backgroundColor = "#FF0000";
} else {
senha.style.backgroundColor = "#FFFFFF";
confirmar.style.backgroundColor = "#FFFFFF";
}
}
var senha = document.getElementById('senha_cadastro');
var confirmar = document.getElementById('confirmarsenha_cadastro');
senha.onkeyup = verificarIgual;
confirmar.onkeyup = verificarIgual;
<input type="text" id="senha_cadastro" />
<input type="text" id="confirmarsenha_cadastro" />
There is also a version of the online example in jsFiddle.
Sources: Using dynamic styling information . p>