1

Getting trouble in accessing set_id method inside MyClass class in HTML elemen button using TypeScript.

Here's my code:

HTML

<button onclick="myClassObject.set_id(1)">Set ID</button>

TypeScript

class MyClass {
    set_id(id: number) {
        console.log(id);
    }
}
let myClassObject = new MyClass();

Error

Uncaught ReferenceError: myClassObject is not defined at HTMLButtonElement.onclick ((index):15)

By the way: I am new in TypeScript. hehe.

Can someone help me?

Update: I'm using webpack to compile my TS

user11657407
  • 312
  • 2
  • 12
Nosaj
  • 51
  • 8
  • 1
    How is your Typescript getting put onto the HTML page? Are you using Webpack or something? – CertainPerformance Jan 17 '20 at 08:17
  • Cannot use typescript directly without compile it to javascript. Are you using any framework like angular? or using any build tools like webpack? – BadPiggie Jan 17 '20 at 08:18
  • I'am using webpack. – Nosaj Jan 17 '20 at 08:19
  • Duplicate of https://stackoverflow.com/questions/59765292/unexpected-modifying-of-the-js-by-webpack/59765377#59765377, but I can't VTC because answer isn't upvoted/accepted. In short, assign `myClassObject` to `window`, or - much better - set the handler properly using Javascript, rather than with an inline attribute – CertainPerformance Jan 17 '20 at 08:24
  • Inspect the generated JS code to find out why your example doesn't work. – Emanuel Vintilă Jan 17 '20 at 08:31

0 Answers0