I have a use case where I am loading the HTML page from the variable.
now I have 3 states to maintain (PENDING, COMPLETED, FAILED)
on each state, I want to show different messages and elements
<script>
var status = "COMPLETED";
function hideBoth() {
document.getElementById("cont1").style.visibility = "hidden";
document.getElementById("cont2").style.visibility = "hidden";
console.log(status, "-=--Status");
if (status === "COMPLETED") {
console.log(status, "INSIDE COMPLETED");
console.log(
document.getElementById("COMPLETED"),
"INSIDE COMPLETED CHECK"
);
document.getElementById(status).innerHTML;
document.getElementById("PENDING").style.visibility = "hidden";
document.getElementById("FAILED").style.visibility = "hidden";
}
if (status === "PENDING") {
document.getElementById("COMPLETED").style.visibility = "hidden";
document.getElementById("PENDING").innerHTML = status;
document.getElementById("FAILED").style.visibility = "hidden";
} else {
document.getElementById("COMPLETED").style.visibility = "hidden";
document.getElementById("PENDING").innerHTML = status;
document.getElementById("FAILED").style.visibility = "hidden";
}
}
</script>
This is the script tag looks like
the entire code is here https://codesandbox.io/s/elated-lamport-fwhqsm?file=/index.html
Where I am missing things, Not so sure about it. Can I get some pointers on what I am missing?
Thanks in advance.
2
Answers
It has two issues:
cont1
andcont2
are not in the HTML, it fails there and stops the executionI forked your code and fixed it here:
https://codesandbox.io/s/elated-breeze-gmh5c4?file=/index.html
Consider the following.
Plus the following HTML Changes.
You can see that now your JavaScript targets one specific HTML Element. Also updating to an
if/else if/else
statement will ensure that one one of the three states is visible.