I have the following defined method:
elclick(id:string) {
console.log('El id es: ' + id);
}
And my html is this:
<a id="0" (click)="elclick(this.id);">Button</a>
What is the way this is done?
I have the following defined method:
elclick(id:string) {
console.log('El id es: ' + id);
}
And my html is this:
<a id="0" (click)="elclick(this.id);">Button</a>
What is the way this is done?
Because of the tags that you have put on to understand that you use angular for this case.
You could use the following:
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: '
<button (click)="onClick($event)" id="test">Click</button>
'
})
export class AppComponent {
onClick(event) {
console.log(event);
console.log(event.srcElement.attributes.id);
var idAttr = event.srcElement.attributes.id;
var value = idAttr.nodeValue;
console.log(value);
}
}
Comparing with the implementation of javascript changes the fact that the this
is not sent but the click event event
is sent directly; this event has all the information you need, in this case the Source srcElement
element that would be your button and its respective attributes attributes
, like id event.srcElement.attributes.id