I have such click event
<button (click)="toggle($event)" class="someclass" id="btn1"></button> <button (click)="toggle($event)" class="someclass" id="btn2"></button>
I am catching the event in my function input param and want to find out what exactly button was clicked.
toggle(event) { }
but event
does not have an id
property.
altKey: false bubbles: true button: 0 buttons: 0 cancelBubble: false cancelable: true clientX: 1198 clientY: 29 ctrlKey: false currentTarget: button#hdrbtn_notificaton.mdl-button.mdl-js-button.mdl-js-ripple-effect.mdl-button--icon defaultPrevented: false detail: 1 eventPhase: 3 fromElement: null isTrusted: true isTrusted: true layerX: -566 layerY: 5 metaKey: false movementX: 0 movementY: 0 offsetX: 22 offsetY: 13 pageX: 1198 pageY: 29 path: Array[13] relatedTarget: null returnValue: true screenX: 1797 screenY: 148 shiftKey: false sourceCapabilities: InputDeviceCapabilities srcElement: span.mdl-button__ripple-container target: span.mdl-button__ripple-container timeStamp: 1458032708743 toElement: span.mdl-button__ripple-container type: "click" view: Window webkitMovementX: 0 webkitMovementY: 0 which: 1 x: 1198 y: 29
How can I find an id
?
UPDATE: Plunkers are all good but in my case I have locally:
event.srcElement.attributes.id
- undefined event.currentTarget.id
- has the value
I am using chrome latest Version 49.0.2623.87 m
Could it be Material Design Lite
thing? because I am using it.
To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.
Here, we will simply take one input text box field with two buttons call "get value" and "reset value". when you click on get value button then we will get value using ElementRef id and when you click on the reset button then we will set the value using ElementRef id.
To go on detection for click outside the component, @HostListener decorator is used in angular. It is a decorator that declares a DOM event to listen for and provides a link with a handler method to run whenever that event occurs.
Angular includes $event that contains the information about an event. The type of $event depends on the target event, e.g., if the target event is a native DOM element event, then it is an object. A component should define the onShow(event) method where the type of the parameter can be KeyboardEvent, MouseEvent, etc.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With