I wanted to fully understand and learn JS Switch statement so I thought of an exercise for myself - to change HTML title depending on what text of a title currently exists.
Here's the JS code:
var zz = document.getElementById("title").textContent;
switch(zz) {
case "Homepage":
console.log(document.getElementById("title").textContent);
document.getElementById("title").textContent = "Case 1";
console.log(document.getElementById("title").textContent + "(HAS CHANGED)")
break;
case "Homepage2":
console.log(document.getElementById("title").textContent);
document.getElementById("title").textContent = "Case 2";
console.log(document.getElementById("title").textContent + "(HAS CHANGED)")
break;
default:
console.log(document.getElementById("title").textContent);
document.getElementById("title").textContent = "Default case";
console.log(document.getElementById("title").textContent + "(HAS CHANGED)")
break;
}
HTML:
<title id="title"> Homepage </title>
Problem is that it always executes the "default" case even though the title fits of of the presented cases. I believe the problem is within the var zz = document.getElementById("title").textContent; because if I declare the text content of it - it works (I don't want to do that as it just overwrites the existing title from HTML). Is it possible to achieve this with switch statement? P.S I don't want to use if/else - I want to do it with switch. Thanks
Homepage?