0

How can I bind to onclick listener a method of dynamically created object?

class PanelWindow {
    constructor(windowMenager) {
        this.windowMenager = windowMenager;

        this.makeDomElements();
        this.setId(windowMenager);
    }

    setId(windowMenager) {
        this.id = windowMenager.getId(this);
        console.log(this.id);
    }

    createDomElements() {
        var pn_header_close = document.createElement("div");
        pn_header_close.onclick = this.closeWindow;
    }

    closeWindow() {
        this.window.style.display = "none";
    }

}

I would like to bind to the pn_header_close.onclick this: this.closeWindow or WindowActions.closeWindow(this.id, this.windowMenager)

sal-k
  • 91
  • 8

1 Answers1

0

in your constructor include:

this.closeWindow = this.closeWindow.bind(this);
Kyle Richardson
  • 5,567
  • 3
  • 17
  • 40