I have a checkbox that when I leave it checked I want it to say Active in green, and when I leave it unlocked I want it to say inactive, the topic is like this, by javascript I did that when checked it says in active green, and when it is unchecked it says inactive, my problem is that every time I uncheck creates a new element, I tried with removeChild and replaceChild and I can not make the active text disappear and the inactive one appears.
This is the base code:
function textoEstado() {
var checkbox = document.getElementById("estadoServicio");
var parrafo = document.createElement("p");
var tdCheckbox = document.getElementById("tdCheckbox");
var activo = document.createTextNode("Activo");
var inactivo = document.createTextNode("Inactivo");
if (checkbox.checked) {
tdCheckbox.appendChild(parrafo).style.color = 'green';
parrafo.appendChild(activo);
} else {
tdCheckbox.appendChild(parrafo).style.color = 'red';
parrafo.appendChild(inactivo);
}
}
This is the code with some attempts to make the active text disappear and the inactive one appear without you passing it creating elements:
//TODO removeChild activo inactivo
function textoEstado() {
var checkbox = document.getElementById("estadoServicio");
var parrafo = document.createElement("p");
var parrafo1 = document.createElement("p");
var tdCheckbox = document.getElementById("tdCheckbox");
var activo = document.createTextNode("Activo");
var inactivo = document.createTextNode("Inactivo");
if (checkbox.checked) {
tdCheckbox.appendChild(parrafo).style.color = 'green';
parrafo.appendChild(activo);
} else {
parrafo.removeChild(parrafo.childNodes[0]);
tdCheckbox.appendChild(parrafo1).style.color = 'red';
parrafo1.appendChild(inactivo);
}
}
Also try this other one and it does not go:
//TODO removeChild activo inactivo
function textoEstado() {
var checkbox = document.getElementById("estadoServicio");
var parrafo = document.createElement("p");
var tdCheckbox = document.getElementById("tdCheckbox");
var activo = document.createTextNode("Activo");
var inactivo = document.createTextNode("Inactivo");
if (checkbox.checked) {
tdCheckbox.appendChild(parrafo).style.color = 'green';
parrafo.appendChild(activo);
} else {
parrafo.removeChild(parrafo.childNodes[0]);
tdCheckbox.appendChild(parrafo).style.color = 'red';
parrafo.appendChild(inactivo);
}
}
Neither of them works for me, if I continue like this I'll have to put an alert, but it would really be a very easy way, thanks for the attention.