onClick calling a method not working (Scheduler)

Hi!

I’m trying to call a method from my onClick function like this:

scheduler.attachEvent("onClick", function (id, e){
    console.log("id: ", id);
    this.callingMethod(id);
    console.log("method called");
return false;
});

But when I do this, it doesn’t work, it only prints “id” and the method never gets called, even if the method it’s a simple console.log like this:

callingMethod(id){
      console.log("id",id);
}

I need to do this because that method it’s supposed to trigger an action that will open a little window with the event information.

Another thing that happens is that when I define a variable for the class for example:

tasks = information about tasks

then when I print it before the onClick function it gives the information stored in the variable, but when I do it inside the function:

scheduler.attachEvent("onClick", function (id, e){
                console.log("id", id);
                console.log("this.tasks", this.tasks);
        return false;
});

it prints undefined.

Why is this all happening?

Hello @Javiera_Meneses,
I’m afraid I still don’t fully understand why you need a method inside the handler. You can create a separate function and use it inside the handler like in the sample:
http://snippet.dhtmlx.com/5/e58676f9b
As for the second question:

then when I print it before the onClick function it gives the information stored in the variable, but when I do it inside the function.

if you want to set the value of “this” to a particular value when calling a function, use call(), or ``apply()‘’:

Here is the implementation:
http://snippet.dhtmlx.com/5/5428b3c34

If you need something different, please describe your question with more details.

1 Like

I also tried what you did, but it didn’t work either. I finally ended up doing this:

scheduler.attachEvent("onClick", this.onSelect.bind(null, this));

where this.onSelect is the method that I wanted to call.

Thanks anyway!

1 Like