Angular2 get clicked element id












55















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.



enter image description here










share|improve this question

























  • 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 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











  • Seems to be a button issue stackoverflow.com/questions/4825295/…

    – Günter Zöchbauer
    Mar 15 '16 at 9:24
















55















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.



enter image description here










share|improve this question

























  • 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 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











  • Seems to be a button issue stackoverflow.com/questions/4825295/…

    – Günter Zöchbauer
    Mar 15 '16 at 9:24














55












55








55


14






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.



enter image description here










share|improve this question
















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.



enter image description here







angular






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 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











  • Seems to be a button issue 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











  • 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











  • 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

















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












8 Answers
8






active

oldest

votes


















102














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?






share|improve this answer





















  • 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













  • 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






  • 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 this var target = event.srcElement.attributes.id || event.currentTarget.id;

    – sreginogemoh
    Mar 15 '16 at 10:54



















21














For TypeScript users:



    toggle(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}





share|improve this answer



















  • 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





















15














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">





share|improve this answer


























  • do you think is it better to avoid of using an id in 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 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



















14














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);
}





share|improve this answer


























  • 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





    To get the button element use event.target.closest('button').

    – Skorunka František
    Jul 22 '17 at 16:02





















3














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
}





share|improve this answer

































    2














    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.






    share|improve this answer































      1














      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






      share|improve this answer



















      • 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













      • just checkout in the Object of event fired where you can see id

        – Pardeep Jain
        Mar 15 '16 at 9:27






      • 1





        @sreginogemoth you can also check: event.target.id it can also have the id value

        – Arthur
        Nov 25 '16 at 16:00





















      1














      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






      share|improve this answer
























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        102














        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?






        share|improve this answer





















        • 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













        • 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






        • 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 this var target = event.srcElement.attributes.id || event.currentTarget.id;

          – sreginogemoh
          Mar 15 '16 at 10:54
















        102














        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?






        share|improve this answer





















        • 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













        • 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






        • 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 this var target = event.srcElement.attributes.id || event.currentTarget.id;

          – sreginogemoh
          Mar 15 '16 at 10:54














        102












        102








        102







        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?






        share|improve this answer















        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?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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.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











        • 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 this var target = event.srcElement.attributes.id || event.currentTarget.id;

          – sreginogemoh
          Mar 15 '16 at 10:54














        • 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













        • 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






        • 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 this var 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













        21














        For TypeScript users:



            toggle(event: Event): void {
        let elementId: string = (event.target as Element).id;
        // do something with the id...
        }





        share|improve this answer



















        • 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


















        21














        For TypeScript users:



            toggle(event: Event): void {
        let elementId: string = (event.target as Element).id;
        // do something with the id...
        }





        share|improve this answer



















        • 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
















        21












        21








        21







        For TypeScript users:



            toggle(event: Event): void {
        let elementId: string = (event.target as Element).id;
        // do something with the id...
        }





        share|improve this answer













        For TypeScript users:



            toggle(event: Event): void {
        let elementId: string = (event.target as Element).id;
        // do something with the id...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 5 '17 at 23:05









        quidkidquidkid

        27823




        27823








        • 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
















        • 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










        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













        15














        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">





        share|improve this answer


























        • do you think is it better to avoid of using an id in 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 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
















        15














        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">





        share|improve this answer


























        • do you think is it better to avoid of using an id in 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 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














        15












        15








        15







        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">





        share|improve this answer















        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">






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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 an id in 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 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



















        • do you think is it better to avoid of using an id in 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 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

















        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











        14














        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);
        }





        share|improve this answer


























        • 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





          To get the button element use event.target.closest('button').

          – Skorunka František
          Jul 22 '17 at 16:02


















        14














        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);
        }





        share|improve this answer


























        • 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





          To get the button element use event.target.closest('button').

          – Skorunka František
          Jul 22 '17 at 16:02
















        14












        14








        14







        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);
        }





        share|improve this answer















        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);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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 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





          To get the button element use event.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






        • 2





          To get the button element use event.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













        3














        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
        }





        share|improve this answer






























          3














          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
          }





          share|improve this answer




























            3












            3








            3







            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
            }





            share|improve this answer















            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
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '18 at 5:37

























            answered Jun 9 '18 at 11:13









            WasiFWasiF

            2,9622841




            2,9622841























                2














                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.






                share|improve this answer




























                  2














                  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.






                  share|improve this answer


























                    2












                    2








                    2







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 26 '18 at 7:54









                    Seyed EhsanSeyed Ehsan

                    1074




                    1074























                        1














                        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






                        share|improve this answer



















                        • 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













                        • just checkout in the Object of event fired where you can see id

                          – Pardeep Jain
                          Mar 15 '16 at 9:27






                        • 1





                          @sreginogemoth you can also check: event.target.id it can also have the id value

                          – Arthur
                          Nov 25 '16 at 16:00


















                        1














                        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






                        share|improve this answer



















                        • 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













                        • just checkout in the Object of event fired where you can see id

                          – Pardeep Jain
                          Mar 15 '16 at 9:27






                        • 1





                          @sreginogemoth you can also check: event.target.id it can also have the id value

                          – Arthur
                          Nov 25 '16 at 16:00
















                        1












                        1








                        1







                        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






                        share|improve this answer













                        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







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 15 '16 at 9:21









                        Pardeep JainPardeep Jain

                        42.4k17104147




                        42.4k17104147








                        • 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













                        • just checkout in the Object of event fired where you can see id

                          – Pardeep Jain
                          Mar 15 '16 at 9:27






                        • 1





                          @sreginogemoth you can also check: event.target.id it can also have the id value

                          – Arthur
                          Nov 25 '16 at 16:00
















                        • 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













                        • just checkout in the Object of event fired where you can see id

                          – Pardeep Jain
                          Mar 15 '16 at 9:27






                        • 1





                          @sreginogemoth you can also check: event.target.id it 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













                        1














                        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






                        share|improve this answer




























                          1














                          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






                          share|improve this answer


























                            1












                            1








                            1







                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 28 '18 at 23:44









                            GregGreg

                            436413




                            436413






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Lallio

                                Futebolista

                                Jornalista