0

I cannot for the life of me figure out why this if statement is not working.

The retrieveInfo function is passed an event, code, and the target div as seen here:

onmouseover="return retreiveInfo(event, 'M003', 'target')"

I must use the code as provided as much as possible for a school assignment.

function retreiveInfo(e, movieCode, div) {
    if(e.pageX) {
        this.x = e.pageX;
    } else {
        this.x = e.clientX;
    }
    if(e.pageY) {
        this.y = e.pageY;
    } else {
        this.y = e.clientY;
    }

    div = document.getElementById(div);

    if(div.style.visibility == "visible") {
        console.log("More Good");
    }
    else if(div.style.visibility == "hidden"){
        console.log("Hidden");
    }
}

1 Answer 1

4

Checking the "style" object only works if the HTML markup included a "style" attribute, or if it's been modified elsewhere by your code. If there's CSS making an object's "visibility" property be "hidden", you won't be able to tell by that method.

There are other ways for objects to not be visible as well:

  • display: none
  • position: absolute; left: -10000px;

etc. An element can be inside another element that's hidden as well. Thus, determining whether a particular element in the DOM is visible is not always a simple task :-)

Now, if some code elsewhere does something like:

document.getElementById(whatever).style.visibility = "hidden";

then your code should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Putting visibility in the HTML markup instead of the CSS worked great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.