-2

I am studing javascript but console say's: document is not defined. In this code:

var btnMenu = document.getElementById('btn-menu');
var nav = document.getElementById('nav');

btnMenu.addEventListener('click'), function(){
  nav.classList.toggle('show');
})

I dont know what is wrong, can you help me?

JustLearning
  • 3,164
  • 3
  • 35
  • 52

1 Answers1

-1

I have tested your code in JsFiddle and you have a bug a parenthesis not needed after 'click'.

var btnMenu = document.getElementById('btn-menu');
var nav = document.getElementById('nav');

btnMenu.addEventListener('click', function(){
  nav.classList.toggle('show');
})
.show {
    width: 250px;
    height: 100px;
    background-color: red;
    text-align: center;
    font-size: 35px;
    color: blue;
}
<div><input type ="submit" id="btn-menu" value="Menu Button"/>
<nav id="nav">The nav</nav>

</div>

So I have added html tags that refer to your code and also added some css code that needed to se the changes from the button when clicked.

Note. This is just an example how can you work with addEventListener and classList.toggle and making your piece of code working.

Llazar
  • 3,167
  • 3
  • 17
  • 24
  • I know you mentioned that at the end of your answer, but the OP problem is something else – Colin Cline Nov 25 '18 at 20:23
  • Problem is `document` is not defined. If this happens when the user clicks, your answer is right not when the user just runs it. You run your code in the right environment. Problem is OP doesn't know in which environment he is running his code. – Colin Cline Nov 25 '18 at 20:36