kriskotoo BG
kriskotoo BG

Reputation: 321

Get class property from jquery callback

I have a class:

class MyClass{
    #arr= [];


    constructor(){
        $("body").on("click", ".myClass", function(e){
           //here, i need to access this.#arr, but unfortunatelly "this" no longer represents MyClass.
        }
    }
}

How would I go about doing this? Thanks in advance!

Upvotes: 1

Views: 55

Answers (1)

Unmitigated
Unmitigated

Reputation: 89264

Use an arrow function instead to preserve the this value, after which you can use e.target to get the element that was clicked.

$("body").on("click", ".myClass", e=>{

});

Upvotes: 1

Related Questions