0

I'm trying to develop some script which toggles the display property of the css linked to my HTML document. This is in order to show/hide the navbar in the document. But when I run this script, the console shows [object HTMLDivElement] for the document.getElementById which is really confusing.

I don't think this is that of a big deal, but my head is kinda cloudy now...

        function navbarCollapse() {
        var navbarCollapsable = document.getElementById('nav-collapsable');

        var navbarCollapsableVisibility = navbarCollapsable.style.display;
        var invisible = "none";

        if (navbarCollapsableVisibility !== invisible) {
            navbarCollapsableVisibility = invisible;
        } else {
            navbarCollapsableVisibility = "block";
        };
    }
2
  • Don't cache navbarCollapsableVisibility or else you're assigning primitives, not references and nothing will happen. Commented Jun 9, 2017 at 18:20
  • Did it @AndrewLi but same results. Nothing changed... Commented Jun 9, 2017 at 18:30

2 Answers 2

3

This is what should be returned. Please refer to MDN's docs for document.getElementById:https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

Return Value

element

is a reference to an Element object, or null if an element with the specified ID is not in the document.

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

3 Comments

No problem, dude.
The element does exist and I've read that resource as well. But still no results...
[object HTMLDivElement] is the representation of the Element Object that is being returned...
1

Just solved it. As @AronF pointed out, [object HTMLDivElement] is what I'm supposed to be seeing. The issue was that I should not have been storing the navbarCollapsable.style.display in a var for some reason. Did this and got through:

function navbarCollapse() {
        var navbarCollapsable = document.getElementById('nav-collapsable');

        if (navbarCollapsable.style.display !== "none") {
            navbarCollapsable.style.display = "none";
        } else {
            navbarCollapsable.style.display = "block";
        };
    }

Comments

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.