Jan Van Looveren Jan Van Looveren - 1 year ago 79
Javascript Question

convert javascript event this with typescript

I am converting my previous JavaScript code to typescript.
Now i am running into the following issue. When i convert the old prototype code to classes and methods inside typescript, i have an issue with events:
Consider following code:

// call this class, provide the parameter of the column
var self = this;
cell.onclick = function () {
self.makeTableSort(this); <== problem with parameter this.

The problem i have is that i need the object of the html element that is clicked.
where as "this" as parameter is not refering to the class object but to the html element.

I have already something tried like below:

// call this class, provide the parameter of the column
cell.onclick = (event) => {
this.makeTableSort(); <== missing parameter,

How do i convert this to TypeScript?

Answer Source

Try this:

cell.onclick = (event) => {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download