0

I'm trying to toggle the display on an element, and whilst I have used the conditional statement using below of '===' on say an ID of an element, which worked, I cannot get this code to work.

Does anyone know why the condition 'if (smallnavbar[0].style.display === 'none')' doesn't work? Is there a way to make the code work (toggle the display between none and flex)?

Thanks in advance!

function displayMenu(){
  var smallnavbar = document.getElementsByClassName("small-navbar-tabs");
  if (smallnavbar[0].style.display === 'none') {
    smallnavbar[0].style.display = 'flex';
  }
  else if (smallnavbar[0].style.display === 'flex') {
    smallnavbar[0].style.display = 'none';
  }
}
4
  • use == instead of ===. === doesn't do any type conversion. Commented Nov 15, 2017 at 18:00
  • Have you tried debugging? Put a line break at the beginning of the function and step through. See what is being saved in smallnavbar, that should shed some light on the situation. Commented Nov 15, 2017 at 18:01
  • 1
    Do the elements actually have the display property set, inline on the element? If they are only set in CSS, this code won't work. You would need to get the computed CSS. Commented Nov 15, 2017 at 18:02
  • Thanks for your input guys, much appreciated!:) Commented Nov 17, 2017 at 16:17

1 Answer 1

2

style.display is referring to the inline style (<div style='display:block'></div>). If the display is set by a class in a stylesheet, the display value will be an empty string.

In such case, neither conditions will be true, and therefore it will seems to you that are "failing".

You can simply double check my hypothesis with a console.log before the if statement (of course assuming the displayMenu is called).

There are several workaround for fixing this. You can:

  1. Check the computed style of the element instead of the inline one:

    function displayMenu(){
      var smallnavbar = document.getElementsByClassName("small-navbar-tabs");
      var display = window.getComputedStyle(smallnavbar[0], "").display;
    
      smallnavbar[0].style.display = display === 'none' ? 'flex' : 'none';
    }
    

    }

  2. Having a separate class name for "flex" and "none" and check that instead of the value (if the browser support or you provide a shim for classList that would be easy:

    smallnavbar[0].classList.toggle("flexible"); 
    
  3. Assuming the default value – e.g. you know that all small-navbar-tabs by default have display:none for example, therefore you could have:

    if (smallnavbar[0].style.display === "flex") {
      smallnavbar[0].style.display = ""; // remove the inline style
    } else {
      // since we're adding the inline style here,
      // it would pass the first check in the future.
      smallnavbar[0].style.display = "flex";
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Hey @ZERO . Thanks for your very complete answer! Thanks for the multitude of workarounds, really interesting to see different pathways to effecting a solution. I'm not entirely sure I understand everything you've explained (my JS knowledge is still quite basic, but I'm improving), but the second workaround looks the nicest, will have to do a bit more research. Tried the first and it does the job, will look at 'classList'. Thanks again, really appreciate you time:)) Have a great weekend!

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.