1

I'm trying to toggle a navbar when you click on a button. I've followed an example but my javascript gives an error: caught TypeError: Cannot read property 'display' of undefined at HTMLDivElement.myFunction (script.js:6) myFunction @ script.js:6

The Code:

var myNavigation = document.getElementsByTagName("nav");

var myFunction = function (){
       if (myNavigation.style.display === "none") {
              myNavigation.style.display === "flex"
       } else {
              myNavigation.style.display === "none"
       }
};

myButton.onclick = myFunction;

2 Answers2

3

You are using document.getElementsByTagName(). This returns an array. You have to target a specific element inside that array.

genius42
  • 243
  • 1
  • 6
1

getElementsByTagName returns a list, not an element.

ray
  • 26,557
  • 5
  • 28
  • 27
  • 1
    It does return a *live* HTMLCollection though, so it doesn't matter what it will find at load time – Bergi Jul 04 '20 at 19:32