0

I'm trying to make a Script that when a user clicks in an image with the class "burger-nav-img" it toggles the class "open" in another elements. My code is:

HTML

<nav class="main-nav">
    <ul class="flex-container">
        <li class="flex-content"><a href="#">Home</a></li>
        <li class="flex-content"><a href="#">Articles</a></li>
        <div class="logo_container flex-container">
            <img src="img\logo_background.png" class="logo flex-content">
        </div>
        <li class="flex-content"><a href="#">Support</a></li>
        <li class="flex-content"><a href="#">Login</a></li>
    </ul>
<a class="burger-nav">
    <img src="img/menu.png" id="burger" class="burger-nav-img">
</a>

JavaScript

document.body.addEventListener("load", clas);
function clas() {
    document.getElementsByClassName("burger-nav-img").addEventListener("load", toggl);
}
function toggl() {
    const burger = document.getElementsByClassName("burger-nav");
    burger.classList.toggle("open");
    const main = document.getElementsByClassName("main-nav");
    main.classList.toggle("open");
}

The problem is that when I run this code an error appears and I can't figure out what to do:

Console

menu.js:7 Uncaught TypeError: Cannot read property 'toggle' of undefined
    at toggl (menu.js:7)
    at clas (menu.js:3)
    at menu.js:1
toggl @ menu.js:7
clas @ menu.js:3
(anonymous) @ menu.js:1

Note: menu.js is the file containing the JavaScript Code;

What should I do? Thank You For Your Attention!

4
  • what is this "open" passed in toggle function? Commented Jul 26, 2018 at 12:44
  • 2
    Your call to addEventListener is incorrect. You're passing the result of calling class() instead of just a reference to the function (clas without the ()). Commented Jul 26, 2018 at 12:44
  • The "open" is the name of the class that I want to toggle. Commented Jul 26, 2018 at 12:45
  • Ok thanks I changed to what you said. But the error still remains appearing Commented Jul 26, 2018 at 12:48

3 Answers 3

4

getElementsByClassName returns an array of elements, and an array doesn't have classList property, and therefore you get undefined.

Use burger[0].classList or use an Id and getElementById that returns a single element.

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

3 Comments

Thanks the error has gone, but it still doesn't work. Any Idea?
You need to debug it step by step. Do you get the element you want in burger? Does the toggle() really toggles the class 'open'? If so, maybe there is something wrong in your css.
I tried console.log("OK") to test if the functions are running and they aren't being called
1

burger returns a collection so you should do something like

for (let i = 0; i < burger.length; i++) { burger[i].classList.toggle("open"); }

1 Comment

Thanks the error has gone, but it still doesn't work. Any Idea?
0

Ok after some research and a lot of attempts to solve the problem I finally got it. This is my final JavaScript Code:

document.getElementById("burger").onclick = toggl;

function toggl() {
    console.log("OK");
    var burger = document.getElementById("burger-nav");
    var main = document.getElementById("main-nav");
    if (burger.classList.contains("open")) {
        console.log("OKEY-DOKEY");
        burger.classList.remove("open");
        main.classList.remove("open");
    } else {
        console.log("OK-OK");
        burger.classList.add("open");
        main.classList.add("open");
    }

}

Thanks to Everyone who helped me! I appreciated It a lot!

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.