Angular2 get clicked element id
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.

|
show 5 more comments
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.

what you want to do withid?
– Pardeep Jain
Mar 15 '16 at 9:15
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
simply pass the static/dynamicidalong with perameters.
– Pardeep Jain
Mar 15 '16 at 9:19
why it is so hard to get anid?
– sreginogemoh
Mar 15 '16 at 9:22
Seems to be abuttonissue stackoverflow.com/questions/4825295/…
– Günter Zöchbauer
Mar 15 '16 at 9:24
|
show 5 more comments
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.

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.

edited Mar 15 '16 at 9:32
sreginogemoh
asked Mar 15 '16 at 9:13
sreginogemohsreginogemoh
2,649124486
2,649124486
what you want to do withid?
– Pardeep Jain
Mar 15 '16 at 9:15
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
simply pass the static/dynamicidalong with perameters.
– Pardeep Jain
Mar 15 '16 at 9:19
why it is so hard to get anid?
– sreginogemoh
Mar 15 '16 at 9:22
Seems to be abuttonissue stackoverflow.com/questions/4825295/…
– Günter Zöchbauer
Mar 15 '16 at 9:24
|
show 5 more comments
what you want to do withid?
– Pardeep Jain
Mar 15 '16 at 9:15
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
simply pass the static/dynamicidalong with perameters.
– Pardeep Jain
Mar 15 '16 at 9:19
why it is so hard to get anid?
– sreginogemoh
Mar 15 '16 at 9:22
Seems to be abuttonissue stackoverflow.com/questions/4825295/…
– Günter Zöchbauer
Mar 15 '16 at 9:24
what you want to do with
id ?– Pardeep Jain
Mar 15 '16 at 9:15
what you want to do with
id ?– Pardeep Jain
Mar 15 '16 at 9:15
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
simply pass the static/dynamic
id along with perameters.– Pardeep Jain
Mar 15 '16 at 9:19
simply pass the static/dynamic
id along with perameters.– Pardeep Jain
Mar 15 '16 at 9:19
why it is so hard to get an
id?– sreginogemoh
Mar 15 '16 at 9:22
why it is so hard to get an
id?– sreginogemoh
Mar 15 '16 at 9:22
Seems to be a
button issue stackoverflow.com/questions/4825295/…– Günter Zöchbauer
Mar 15 '16 at 9:24
Seems to be a
button issue stackoverflow.com/questions/4825295/…– Günter Zöchbauer
Mar 15 '16 at 9:24
|
show 5 more comments
8 Answers
8
active
oldest
votes
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:
- How can I make event.srcElement work in Firefox and what does it mean?
1
event.srcElement.attributes.idis undefined do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),currentTargetis undefined :-( Which browser do you use?
– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
Chrome:angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined
– sreginogemoh
Mar 15 '16 at 10:22
4
I endup of using thisvar target = event.srcElement.attributes.id || event.currentTarget.id;
– sreginogemoh
Mar 15 '16 at 10:54
|
show 11 more comments
For TypeScript users:
toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}
3
This should be the accepted answer in my opinion.However it can beconst elementId: string = (event.target as Element).id;
– Harish
Nov 26 '17 at 5:10
add a comment |
You could just pass a static value (or a variable from *ngFor or whatever)
<button (click)="toggle(1)" class="someclass">
<button (click)="toggle(2)" class="someclass">
do you think is it better to avoid of using anidin such case?
– sreginogemoh
Mar 15 '16 at 9:26
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
Instead of usingidjust pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.
– Yuri
Jan 9 '17 at 13:45
add a comment |
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);
}
This might be a tricky: if your button has some HTML content, like<button ...><i class="fa fa-check"></i></button>and you actually click on thatielement, which is FontAwasome component, then the event.target is not abuttonbut theielement.
– Skorunka František
Jul 22 '17 at 15:57
2
To get thebuttonelement useevent.target.closest('button').
– Skorunka František
Jul 22 '17 at 16:02
add a comment |
When your HTMLElement doesn't have an id, name or class to call,
then use
<input type="text" (click)="selectedInput($event)">
selectedInput(event: MouseEvent) {
log(event.srcElement) // HTMLElement
}
add a comment |
If you want to have access to the id attribute of the button in angular 6 follow this code
`@Component({
selector: 'my-app',
template: `
<button (click)="clicked($event)" id="myId">Click Me</button>
`
})
export class AppComponent {
clicked(event) {
const target = event.target || event.srcElement || event.currentTarget;
const idAttr = target.attributes.id;
const value = idAttr.nodeValue;
}
}`
your id in the value,
the value of value is myId.
add a comment |
do like this simply: (as said in comment here is with example with two methods)
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="checkEvent($event,'a')" id="abc" class="def">Display Toastr</button>
<button (click)="checkEvent($event,'b')" id="abc1" class="def1">Display Toastr1</button>
`
})
export class AppComponent {
checkEvent(event, id){
console.log(event, id, event.srcElement.attributes.id);
}
}
demo: http://plnkr.co/edit/5kJaj9D13srJxmod213r?p=preview
4
event.srcElement.attributes.idis undefined in my case do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object ofeventfired where you can seeid
– Pardeep Jain
Mar 15 '16 at 9:27
1
@sreginogemoth you can also check:event.target.idit can also have the id value
– Arthur
Nov 25 '16 at 16:00
add a comment |
There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button #btn1 (click)="toggle(btn1)" class="someclass" id="btn1">Button 1</button>
<button #btn2 (click)="toggle(btn2)" class="someclass" id="btn2">Button 2</button>
`
})
export class AppComponent {
buttonValue: string;
toggle(button) {
this.buttonValue = button.id;
}
}
StackBlitz demo
Technically, you don't need to find the button that was clicked, because you have passed the actual element.
Angular guidance
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36006894%2fangular2-get-clicked-element-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
- How can I make event.srcElement work in Firefox and what does it mean?
1
event.srcElement.attributes.idis undefined do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),currentTargetis undefined :-( Which browser do you use?
– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
Chrome:angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined
– sreginogemoh
Mar 15 '16 at 10:22
4
I endup of using thisvar target = event.srcElement.attributes.id || event.currentTarget.id;
– sreginogemoh
Mar 15 '16 at 10:54
|
show 11 more comments
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:
- How can I make event.srcElement work in Firefox and what does it mean?
1
event.srcElement.attributes.idis undefined do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),currentTargetis undefined :-( Which browser do you use?
– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
Chrome:angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined
– sreginogemoh
Mar 15 '16 at 10:22
4
I endup of using thisvar target = event.srcElement.attributes.id || event.currentTarget.id;
– sreginogemoh
Mar 15 '16 at 10:54
|
show 11 more comments
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:
- How can I make event.srcElement work in Firefox and what does it mean?
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:
- How can I make event.srcElement work in Firefox and what does it mean?
edited May 23 '17 at 10:31
Community♦
11
11
answered Mar 15 '16 at 9:19
Thierry TemplierThierry Templier
148k29321300
148k29321300
1
event.srcElement.attributes.idis undefined do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),currentTargetis undefined :-( Which browser do you use?
– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
Chrome:angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined
– sreginogemoh
Mar 15 '16 at 10:22
4
I endup of using thisvar target = event.srcElement.attributes.id || event.currentTarget.id;
– sreginogemoh
Mar 15 '16 at 10:54
|
show 11 more comments
1
event.srcElement.attributes.idis undefined do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),currentTargetis undefined :-( Which browser do you use?
– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
Chrome:angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined
– sreginogemoh
Mar 15 '16 at 10:22
4
I endup of using thisvar target = event.srcElement.attributes.id || event.currentTarget.id;
– sreginogemoh
Mar 15 '16 at 10:54
1
1
event.srcElement.attributes.id is undefined do not know why... but I found an id in event.currentTarget.id– sreginogemoh
Mar 15 '16 at 9:25
event.srcElement.attributes.id is undefined do not know why... but I found an id in event.currentTarget.id– sreginogemoh
Mar 15 '16 at 9:25
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),
currentTarget is undefined :-( Which browser do you use?– Thierry Templier
Mar 15 '16 at 9:27
Really? Did you see my plunkr? It's what I use and it's not undefined... In my case (Chrome),
currentTarget is undefined :-( Which browser do you use?– Thierry Templier
Mar 15 '16 at 9:27
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
In fact, it's either srcElement or target according browsers. See this question: stackoverflow.com/questions/5301643/…
– Thierry Templier
Mar 15 '16 at 9:30
3
3
Chrome:
angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined– sreginogemoh
Mar 15 '16 at 10:22
Chrome:
angular2.dev.js:23597 TypeError: Cannot read property 'nodeValue' of undefined– sreginogemoh
Mar 15 '16 at 10:22
4
4
I endup of using this
var target = event.srcElement.attributes.id || event.currentTarget.id;– sreginogemoh
Mar 15 '16 at 10:54
I endup of using this
var target = event.srcElement.attributes.id || event.currentTarget.id;– sreginogemoh
Mar 15 '16 at 10:54
|
show 11 more comments
For TypeScript users:
toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}
3
This should be the accepted answer in my opinion.However it can beconst elementId: string = (event.target as Element).id;
– Harish
Nov 26 '17 at 5:10
add a comment |
For TypeScript users:
toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}
3
This should be the accepted answer in my opinion.However it can beconst elementId: string = (event.target as Element).id;
– Harish
Nov 26 '17 at 5:10
add a comment |
For TypeScript users:
toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}
For TypeScript users:
toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}
answered Apr 5 '17 at 23:05
quidkidquidkid
27823
27823
3
This should be the accepted answer in my opinion.However it can beconst elementId: string = (event.target as Element).id;
– Harish
Nov 26 '17 at 5:10
add a comment |
3
This should be the accepted answer in my opinion.However it can beconst elementId: string = (event.target as Element).id;
– Harish
Nov 26 '17 at 5:10
3
3
This should be the accepted answer in my opinion.However it can be
const elementId: string = (event.target as Element).id;– Harish
Nov 26 '17 at 5:10
This should be the accepted answer in my opinion.However it can be
const elementId: string = (event.target as Element).id;– Harish
Nov 26 '17 at 5:10
add a comment |
You could just pass a static value (or a variable from *ngFor or whatever)
<button (click)="toggle(1)" class="someclass">
<button (click)="toggle(2)" class="someclass">
do you think is it better to avoid of using anidin such case?
– sreginogemoh
Mar 15 '16 at 9:26
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
Instead of usingidjust pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.
– Yuri
Jan 9 '17 at 13:45
add a comment |
You could just pass a static value (or a variable from *ngFor or whatever)
<button (click)="toggle(1)" class="someclass">
<button (click)="toggle(2)" class="someclass">
do you think is it better to avoid of using anidin such case?
– sreginogemoh
Mar 15 '16 at 9:26
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
Instead of usingidjust pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.
– Yuri
Jan 9 '17 at 13:45
add a comment |
You could just pass a static value (or a variable from *ngFor or whatever)
<button (click)="toggle(1)" class="someclass">
<button (click)="toggle(2)" class="someclass">
You could just pass a static value (or a variable from *ngFor or whatever)
<button (click)="toggle(1)" class="someclass">
<button (click)="toggle(2)" class="someclass">
edited Apr 27 '17 at 16:49
answered Mar 15 '16 at 9:19
Günter ZöchbauerGünter Zöchbauer
335k721017947
335k721017947
do you think is it better to avoid of using anidin such case?
– sreginogemoh
Mar 15 '16 at 9:26
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
Instead of usingidjust pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.
– Yuri
Jan 9 '17 at 13:45
add a comment |
do you think is it better to avoid of using anidin such case?
– sreginogemoh
Mar 15 '16 at 9:26
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
Instead of usingidjust pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.
– Yuri
Jan 9 '17 at 13:45
do you think is it better to avoid of using an
id in such case?– sreginogemoh
Mar 15 '16 at 9:26
do you think is it better to avoid of using an
id in such case?– sreginogemoh
Mar 15 '16 at 9:26
1
1
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
If the only purpose is to pass it as event parameter then I wouldn't use an id.
– Günter Zöchbauer
Mar 15 '16 at 9:28
1
1
Instead of using
id just pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.– Yuri
Jan 9 '17 at 13:45
Instead of using
id just pick an index from an array or so. It's often surprising that we overlook the most straight-forward solutions so frequently.– Yuri
Jan 9 '17 at 13:45
add a comment |
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);
}
This might be a tricky: if your button has some HTML content, like<button ...><i class="fa fa-check"></i></button>and you actually click on thatielement, which is FontAwasome component, then the event.target is not abuttonbut theielement.
– Skorunka František
Jul 22 '17 at 15:57
2
To get thebuttonelement useevent.target.closest('button').
– Skorunka František
Jul 22 '17 at 16:02
add a comment |
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);
}
This might be a tricky: if your button has some HTML content, like<button ...><i class="fa fa-check"></i></button>and you actually click on thatielement, which is FontAwasome component, then the event.target is not abuttonbut theielement.
– Skorunka František
Jul 22 '17 at 15:57
2
To get thebuttonelement useevent.target.closest('button').
– Skorunka František
Jul 22 '17 at 16:02
add a comment |
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);
}
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);
}
edited Feb 28 '17 at 21:49
ShellZero
1,71912339
1,71912339
answered Mar 15 '16 at 9:51
micronyksmicronyks
36.9k1275112
36.9k1275112
This might be a tricky: if your button has some HTML content, like<button ...><i class="fa fa-check"></i></button>and you actually click on thatielement, which is FontAwasome component, then the event.target is not abuttonbut theielement.
– Skorunka František
Jul 22 '17 at 15:57
2
To get thebuttonelement useevent.target.closest('button').
– Skorunka František
Jul 22 '17 at 16:02
add a comment |
This might be a tricky: if your button has some HTML content, like<button ...><i class="fa fa-check"></i></button>and you actually click on thatielement, which is FontAwasome component, then the event.target is not abuttonbut theielement.
– Skorunka František
Jul 22 '17 at 15:57
2
To get thebuttonelement useevent.target.closest('button').
– Skorunka František
Jul 22 '17 at 16:02
This might be a tricky: if your button has some HTML content, like
<button ...><i class="fa fa-check"></i></button> and you actually click on that i element, which is FontAwasome component, then the event.target is not a button but the i element.– Skorunka František
Jul 22 '17 at 15:57
This might be a tricky: if your button has some HTML content, like
<button ...><i class="fa fa-check"></i></button> and you actually click on that i element, which is FontAwasome component, then the event.target is not a button but the i element.– Skorunka František
Jul 22 '17 at 15:57
2
2
To get the
button element use event.target.closest('button').– Skorunka František
Jul 22 '17 at 16:02
To get the
button element use event.target.closest('button').– Skorunka František
Jul 22 '17 at 16:02
add a comment |
When your HTMLElement doesn't have an id, name or class to call,
then use
<input type="text" (click)="selectedInput($event)">
selectedInput(event: MouseEvent) {
log(event.srcElement) // HTMLElement
}
add a comment |
When your HTMLElement doesn't have an id, name or class to call,
then use
<input type="text" (click)="selectedInput($event)">
selectedInput(event: MouseEvent) {
log(event.srcElement) // HTMLElement
}
add a comment |
When your HTMLElement doesn't have an id, name or class to call,
then use
<input type="text" (click)="selectedInput($event)">
selectedInput(event: MouseEvent) {
log(event.srcElement) // HTMLElement
}
When your HTMLElement doesn't have an id, name or class to call,
then use
<input type="text" (click)="selectedInput($event)">
selectedInput(event: MouseEvent) {
log(event.srcElement) // HTMLElement
}
edited Oct 2 '18 at 5:37
answered Jun 9 '18 at 11:13
WasiFWasiF
2,9622841
2,9622841
add a comment |
add a comment |
If you want to have access to the id attribute of the button in angular 6 follow this code
`@Component({
selector: 'my-app',
template: `
<button (click)="clicked($event)" id="myId">Click Me</button>
`
})
export class AppComponent {
clicked(event) {
const target = event.target || event.srcElement || event.currentTarget;
const idAttr = target.attributes.id;
const value = idAttr.nodeValue;
}
}`
your id in the value,
the value of value is myId.
add a comment |
If you want to have access to the id attribute of the button in angular 6 follow this code
`@Component({
selector: 'my-app',
template: `
<button (click)="clicked($event)" id="myId">Click Me</button>
`
})
export class AppComponent {
clicked(event) {
const target = event.target || event.srcElement || event.currentTarget;
const idAttr = target.attributes.id;
const value = idAttr.nodeValue;
}
}`
your id in the value,
the value of value is myId.
add a comment |
If you want to have access to the id attribute of the button in angular 6 follow this code
`@Component({
selector: 'my-app',
template: `
<button (click)="clicked($event)" id="myId">Click Me</button>
`
})
export class AppComponent {
clicked(event) {
const target = event.target || event.srcElement || event.currentTarget;
const idAttr = target.attributes.id;
const value = idAttr.nodeValue;
}
}`
your id in the value,
the value of value is myId.
If you want to have access to the id attribute of the button in angular 6 follow this code
`@Component({
selector: 'my-app',
template: `
<button (click)="clicked($event)" id="myId">Click Me</button>
`
})
export class AppComponent {
clicked(event) {
const target = event.target || event.srcElement || event.currentTarget;
const idAttr = target.attributes.id;
const value = idAttr.nodeValue;
}
}`
your id in the value,
the value of value is myId.
answered Sep 26 '18 at 7:54
Seyed EhsanSeyed Ehsan
1074
1074
add a comment |
add a comment |
do like this simply: (as said in comment here is with example with two methods)
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="checkEvent($event,'a')" id="abc" class="def">Display Toastr</button>
<button (click)="checkEvent($event,'b')" id="abc1" class="def1">Display Toastr1</button>
`
})
export class AppComponent {
checkEvent(event, id){
console.log(event, id, event.srcElement.attributes.id);
}
}
demo: http://plnkr.co/edit/5kJaj9D13srJxmod213r?p=preview
4
event.srcElement.attributes.idis undefined in my case do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object ofeventfired where you can seeid
– Pardeep Jain
Mar 15 '16 at 9:27
1
@sreginogemoth you can also check:event.target.idit can also have the id value
– Arthur
Nov 25 '16 at 16:00
add a comment |
do like this simply: (as said in comment here is with example with two methods)
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="checkEvent($event,'a')" id="abc" class="def">Display Toastr</button>
<button (click)="checkEvent($event,'b')" id="abc1" class="def1">Display Toastr1</button>
`
})
export class AppComponent {
checkEvent(event, id){
console.log(event, id, event.srcElement.attributes.id);
}
}
demo: http://plnkr.co/edit/5kJaj9D13srJxmod213r?p=preview
4
event.srcElement.attributes.idis undefined in my case do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object ofeventfired where you can seeid
– Pardeep Jain
Mar 15 '16 at 9:27
1
@sreginogemoth you can also check:event.target.idit can also have the id value
– Arthur
Nov 25 '16 at 16:00
add a comment |
do like this simply: (as said in comment here is with example with two methods)
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="checkEvent($event,'a')" id="abc" class="def">Display Toastr</button>
<button (click)="checkEvent($event,'b')" id="abc1" class="def1">Display Toastr1</button>
`
})
export class AppComponent {
checkEvent(event, id){
console.log(event, id, event.srcElement.attributes.id);
}
}
demo: http://plnkr.co/edit/5kJaj9D13srJxmod213r?p=preview
do like this simply: (as said in comment here is with example with two methods)
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button (click)="checkEvent($event,'a')" id="abc" class="def">Display Toastr</button>
<button (click)="checkEvent($event,'b')" id="abc1" class="def1">Display Toastr1</button>
`
})
export class AppComponent {
checkEvent(event, id){
console.log(event, id, event.srcElement.attributes.id);
}
}
demo: http://plnkr.co/edit/5kJaj9D13srJxmod213r?p=preview
answered Mar 15 '16 at 9:21
Pardeep JainPardeep Jain
42.4k17104147
42.4k17104147
4
event.srcElement.attributes.idis undefined in my case do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object ofeventfired where you can seeid
– Pardeep Jain
Mar 15 '16 at 9:27
1
@sreginogemoth you can also check:event.target.idit can also have the id value
– Arthur
Nov 25 '16 at 16:00
add a comment |
4
event.srcElement.attributes.idis undefined in my case do not know why... but I found anidinevent.currentTarget.id
– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object ofeventfired where you can seeid
– Pardeep Jain
Mar 15 '16 at 9:27
1
@sreginogemoth you can also check:event.target.idit can also have the id value
– Arthur
Nov 25 '16 at 16:00
4
4
event.srcElement.attributes.id is undefined in my case do not know why... but I found an id in event.currentTarget.id– sreginogemoh
Mar 15 '16 at 9:26
event.srcElement.attributes.id is undefined in my case do not know why... but I found an id in event.currentTarget.id– sreginogemoh
Mar 15 '16 at 9:26
just checkout in the Object of
event fired where you can see id– Pardeep Jain
Mar 15 '16 at 9:27
just checkout in the Object of
event fired where you can see id– Pardeep Jain
Mar 15 '16 at 9:27
1
1
@sreginogemoth you can also check:
event.target.id it can also have the id value– Arthur
Nov 25 '16 at 16:00
@sreginogemoth you can also check:
event.target.id it can also have the id value– Arthur
Nov 25 '16 at 16:00
add a comment |
There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button #btn1 (click)="toggle(btn1)" class="someclass" id="btn1">Button 1</button>
<button #btn2 (click)="toggle(btn2)" class="someclass" id="btn2">Button 2</button>
`
})
export class AppComponent {
buttonValue: string;
toggle(button) {
this.buttonValue = button.id;
}
}
StackBlitz demo
Technically, you don't need to find the button that was clicked, because you have passed the actual element.
Angular guidance
add a comment |
There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button #btn1 (click)="toggle(btn1)" class="someclass" id="btn1">Button 1</button>
<button #btn2 (click)="toggle(btn2)" class="someclass" id="btn2">Button 2</button>
`
})
export class AppComponent {
buttonValue: string;
toggle(button) {
this.buttonValue = button.id;
}
}
StackBlitz demo
Technically, you don't need to find the button that was clicked, because you have passed the actual element.
Angular guidance
add a comment |
There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button #btn1 (click)="toggle(btn1)" class="someclass" id="btn1">Button 1</button>
<button #btn2 (click)="toggle(btn2)" class="someclass" id="btn2">Button 2</button>
`
})
export class AppComponent {
buttonValue: string;
toggle(button) {
this.buttonValue = button.id;
}
}
StackBlitz demo
Technically, you don't need to find the button that was clicked, because you have passed the actual element.
Angular guidance
There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<button #btn1 (click)="toggle(btn1)" class="someclass" id="btn1">Button 1</button>
<button #btn2 (click)="toggle(btn2)" class="someclass" id="btn2">Button 2</button>
`
})
export class AppComponent {
buttonValue: string;
toggle(button) {
this.buttonValue = button.id;
}
}
StackBlitz demo
Technically, you don't need to find the button that was clicked, because you have passed the actual element.
Angular guidance
answered Nov 28 '18 at 23:44
GregGreg
436413
436413
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36006894%2fangular2-get-clicked-element-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what you want to do with
id?– Pardeep Jain
Mar 15 '16 at 9:15
I have two buttons firing same click function. So I need to find out which one was clicked
– sreginogemoh
Mar 15 '16 at 9:18
simply pass the static/dynamic
idalong with perameters.– Pardeep Jain
Mar 15 '16 at 9:19
why it is so hard to get an
id?– sreginogemoh
Mar 15 '16 at 9:22
Seems to be a
buttonissue stackoverflow.com/questions/4825295/…– Günter Zöchbauer
Mar 15 '16 at 9:24