admin管理员组文章数量:1426803
I am trying to get the attribute value of the element clicked in my javascript code but for some reason the value I am getting is undefined.
This is the code:
HTML:
<c:button buttonLabel="Skype" buttonClass="valmetSecondaryButton noMargin" id="I_WANT_THIS_VALUE" onclick="{!c.openSkype}"/>
JS:
console.log(event.currentTarget.id);
This is working on Internet Explorer, havent tested in Firefox, but not working in Chrome. Also just so you know I am using Lightening Component, Salesforce!
I am trying to get the attribute value of the element clicked in my javascript code but for some reason the value I am getting is undefined.
This is the code:
HTML:
<c:button buttonLabel="Skype" buttonClass="valmetSecondaryButton noMargin" id="I_WANT_THIS_VALUE" onclick="{!c.openSkype}"/>
JS:
console.log(event.currentTarget.id);
This is working on Internet Explorer, havent tested in Firefox, but not working in Chrome. Also just so you know I am using Lightening Component, Salesforce!
Share Improve this question asked Jan 25, 2018 at 9:42 MizlulMizlul 2,29010 gold badges52 silver badges105 bronze badges 2- 1 Something like this asked before: stackoverflow./questions/32456290/… Thanks – Kapil Yadav Commented Jan 25, 2018 at 9:50
- @kapilyadav i tried that link, no success, could you show me with a code or smth how do I get fixed this, I understood that in my case i am applying event bubbling but I dont know how can I make such that when the button is clicked stop it there and return id attribute of this button! – Mizlul Commented Jan 25, 2018 at 13:32
2 Answers
Reset to default 3Although not directly related to salesforce-lightning
as your question is tagged, this thread is the first result when searching for evt currentTarget undefined
so i'll leave my general answer here:
I've encountered a weird issue where event.currentTarget
was undefined
when it definitely shouldn't be - and it even appeared to be defined at the start of the event handler, but just disappear later.
It turns out that evt.currentTarget
may disappear from the event when execution is passed on from the event - for example when await
ing a network request or trying to access the event from a setTimeout.
From MDN docs:
Note: The value of event.currentTarget is only available while the event is being handled. If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null.
This can also be demonstrated with the following code at the top of the event handler:
console.log(e.currentTarget);
setTimeout(()=> console.log(e.currentTarget), 0);
If you want to run some async actions before using event.currentTarget
, you can probably just save the value of event.currentTarget
into a variable before the async actions, and then later use the variable.
let elem = event.currentTarget;
await doSomethingAsync();
console.log(elem);
Problem solved:
<span id="{!v.teamMember.email}" onclick="{!c.openSkype}">
<c:button buttonLabel="Skype" buttonClass="valmetSecondaryButton noMargin"/>
</span>
Thanks you all for your interest on solving this!
This link helped me solve the problem: https://salesforce.stackexchange./questions/160830/lighting-ponent-get-html-button-id-in-controller-js-working-in-firefox-not
Event Bubbling
本文标签: javascripteventcurrentTarget getting undefined on ChromeStack Overflow
版权声明:本文标题:javascript - event.currentTarget getting undefined on Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745479171a2660101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论