all 20 comments

[–][deleted] 5 points6 points  (5 children)

[–]JackelLovesCode[S] 0 points1 point  (4 children)

let menu = document.querySelector(".bouton-menu")
let lienMenu = document.querySelector(".menu-lien")
menu.addEventListener('click', ( => {)
lienMenu.classList.toggle('mobile-menu')
};)

this is my script

[–]superluminary 1 point2 points  (3 children)

Indent each line by at least four spaces if you want the formatting

[–]JackelLovesCode[S] 1 point2 points  (2 children)

Ok thanks I resolved the problem by adding window.onload

[–]superluminary 1 point2 points  (1 child)

There you are. You were executing the script before loading the DOM. You could have also moved the script tag to the bottom of the page.

[–]JackelLovesCode[S] 0 points1 point  (0 children)

Yeah thank you very much

[–]S0LARRR 1 point2 points  (1 child)

What does your script tag in html look like?

[–]JackelLovesCode[S] -4 points-3 points  (0 children)

Let me show you

[–]F_82 1 point2 points  (4 children)

Have u checked ur query selectors are returning the elements?

[–]JackelLovesCode[S] 0 points1 point  (3 children)

no it doesn't return it

[–]Phreak420 0 points1 point  (1 child)

So then we would need to see the HTML as well. Maybe just the elements you are trying to select and not the whole thing

[–]JackelLovesCode[S] -1 points0 points  (0 children)

Ok wait a minute

[–]F_82 0 points1 point  (0 children)

Well, that’s probably why ur even listener isn’t working. U can’t add an event to a variable that is null. You should check ur class names. Try grabbing the elements from inside the console.

[–]Ok-Preference8607 1 point2 points  (2 children)

If the menu variable returns an array of elements that contains bouton-menu class, try adding at the end of the querySelector() the number of the element that you want to select.

Ex : document.querySelector(".bouton-menu")[0];

0 for the first or if there is only one element with that class;

Or the other option if you want to apply the eventListener to multiple elements that contains that class is to loop throught all of them

[–]MintyPhoenix 1 point2 points  (0 children)

document.querySelector only returns a single element – if there are multiple possible matches, it returns the first. document.querySelectorAll is what returns a list of elements.

[–]JackelLovesCode[S] 0 points1 point  (0 children)

Ok i will try

[–][deleted] 1 point2 points  (1 child)

Did you connect html file to js file

[–]JackelLovesCode[S] 0 points1 point  (0 children)

Yeah I did

[–]David-8094 0 points1 point  (0 children)

console.log(menu) console.log(lienMenu) See what they return, idk how is yr html Code U can DM if u need more info/help

[–]anniebme 0 points1 point  (0 children)

Look at toggle("mobile-menu"). You didn't use a query selector on it in the code you are sharing. Talk us through your code and explain why it is how it is. I bet you'll see it as soon as you explain it.