Jacob Sartorius Jacob Sartorius - 3 months ago 5
Javascript Question

Else function is not functioning. Help would be appreciated.

I've been trying to get this code running for quite a while, but with no luck.

function clicck() {
if(f == 129) {
document.getElementById("displayy").style.display = "none";
return f;
} else {
document.getElementById("displayy").style.display = "inline";
}
}
var lkj = document.getElementById("displayy");
var f = lkj.offsetHeight;


This code sets the display: none for "displayy" elements and the value of f is set to 0, when the first time the function is activated. However, if the function is activated once the f variable is equal to 0, the else function doesn't set the display: inline. What is the reason behind this error?

Answer

Your code to retrieve the value of f is outside the function, so it only gets set once. Try this instead:

function clicck() {
    var lkj = document.getElementById("displayy");
    var f = lkj.offsetHeight;
    if(f == 129) {
        lkj.style.display = "none";
        return f;
    } else {
        lkj.style.display = "inline";
    }
}