9

I am trying to use typescript in my application. for the same I am doing an POC and in POC i want to call a function Defined in TypeScript class on button click.

Is it possible to call the function? If yes then how?

So far I have seen examples where the functions are called on Page load only. I need to call a function on certain event.

Thanks.

2 Answers 2

6

I need to call a function on certain event.

If the function is global e.g. TypeScript:

function foo(){
   alert('foo');
}

Just use onclick e.g. html:

<button onclick="foo()">click me!</button>

Remember TypeScript is (almost) JavaScript.

However I recommend using a framework like angular with TypeScript. For maintainability. As an example take a look at the browser quickstart.

Sign up to request clarification or add additional context in comments.

1 Comment

this is not working at all for my use case with typescript.
-3

HTML WILL BE:

<button (click)="myMethod($event)">Click Event</button>

function in .ts file

private myMethod(event:any):void
{
   alert(JSON.stringify(event));
}

1 Comment

need help on this(type script): My code: const new_H1: HTMLButtonElement = document.createElement("button"); new_H1.innerText="abc"; <b>new_H1.onclick(alert("abc"):void);</b> this.target.appendChild(new_H1); bold line is giving the error(syntax)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.