You can use its interface HTMLButtonElement
that inherits from its parent HTMLElement
!
This way you will be able to have auto-completion...
<button (click)="toggle($event)" class="someclass otherClass" id="btn1"></button>
toggle(event: MouseEvent) {
const button = event.target as HTMLButtonElement;
console.log(button.id);
console.log(button.className);
}
To see all list of HTMLElement from the World Wide Web Consortium (W3C) documentation
StackBlitz demo
If you want to have access to the id
attribute of the button you can leverage the srcElement
property of the event:
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="onClick($event)" id="test">Click</button>
`
})
export class AppComponent {
onClick(event) {
var target = event.target || event.srcElement || event.currentTarget;
var idAttr = target.attributes.id;
var value = idAttr.nodeValue;
}
}
See this plunkr: https://plnkr.co/edit/QGdou4?p=preview.
See this question:
Finally found the simplest way:
<button (click)="toggle($event)" class="someclass" id="btn1"></button>
<button (click)="toggle($event)" class="someclass" id="btn2"></button>
toggle(event) {
console.log(event.target.id);
}