0

I want to Call the functions handleopenfirst() and handlecloseall() inside the init() function.

If I do I get the Error:

Uncaught TypeError: this.handlecloseall is not a function at HTMLDivElement.<anonymous>

Thank you,

Here the JS Code:

class MLMenu {
    constructor(menuEL){
        this.MenuEl = menuEl;
        this.buttonopenclose = this.MenuEl.querySelector("#buttonopenandclose");
        this.navigationopen = false;
        console.log(this.MenuEl)
        console.log(this.buttonopenclose)
        this.init();
        
    }

    

    init(){
        //add Eventlistener to Open and Close Button
        var button = this.buttonopenclose;
        button.addEventListener('click', function(){
            if (this.navigationopen === false) {
                this.navigationopen = true;
                button.classList.add('open');
                button.classList.remove('close');
                
                this.handleopenfirst();

            

                console.log("Navigation should be opend now");
            } else {
                this.navigationopen = false;
                button.classList.add('close');
                button.classList.remove('open');

                this.handlecloseall();

                console.log("Navigation should be closed now");
            }
        
        });
    }

    handleopenfirst(){
        console.log('hallo');
    }

    handlecloseall(){
        console.log('hallo');
    }
    
}
Carlo
  • 112
  • 5
  • 2
    'This' in your button eventhandler points to the button and not your class. Put your class in a variable in the init phase. So 'var self = this". Then use self.handleopenfirst instead :) – Wimanicesir Apr 09 '21 at 12:32
  • Kinda says it in the error message: "at HTMLDivElement." The anonymous function is the event listener function you're adding. So use an arrow function. – Heretic Monkey Apr 09 '21 at 12:32
  • @Wimanicesir No. Just use an arrow function. OP is already using ES6 – Bergi Apr 09 '21 at 13:01

0 Answers0