Why i get different output values with the same declared constants on an event?
I have a very weird issue going on. I create 2 event listeners one for each button and the i just console.log(e.target.className)
The button have also inside them an
<i class='...'/>
. The weird think is that when i target the elements like this :
const btn1 = something1;
const btn2 = something2;
It gives me a different result from declaring it like this
const btn1 = something1, btn2 = something2;
What is going on with this? Do these two ways of the declaration have to do anything with the scope?
What would be preferable and under what circumstances should I use the one rather than the other?
Here is a pen for a quick review.
UPDATE
I also came across another weird 'issue'.
When I try to make a declaration of the two of the above variables like this
const /* hit enter here */
btn1 = something1, btn2 = something2
i get as a result the class vale of the button and not the inner <i class='...'/>
javascript javascript-events constants declaration
|
show 1 more comment
I have a very weird issue going on. I create 2 event listeners one for each button and the i just console.log(e.target.className)
The button have also inside them an
<i class='...'/>
. The weird think is that when i target the elements like this :
const btn1 = something1;
const btn2 = something2;
It gives me a different result from declaring it like this
const btn1 = something1, btn2 = something2;
What is going on with this? Do these two ways of the declaration have to do anything with the scope?
What would be preferable and under what circumstances should I use the one rather than the other?
Here is a pen for a quick review.
UPDATE
I also came across another weird 'issue'.
When I try to make a declaration of the two of the above variables like this
const /* hit enter here */
btn1 = something1, btn2 = something2
i get as a result the class vale of the button and not the inner <i class='...'/>
javascript javascript-events constants declaration
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
Ah, I see what's happening, this is nothing to do with theconst
, it's how events propagate,. A listener will attach to all siblings,<button><i>
so it depends on were you click the button, if you hit thei
tag, or thebutton
tag.. Try usingcurrentTarget
this is the element you attached the event listener onto.
– Keith
Nov 13 '18 at 13:56
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
If you want the btn, it's justcurrentTarget.className
, if you want the<i>
,currentTarget.querySelector("i").className
yes, should have said child elements,..
– Keith
Nov 13 '18 at 14:03
|
show 1 more comment
I have a very weird issue going on. I create 2 event listeners one for each button and the i just console.log(e.target.className)
The button have also inside them an
<i class='...'/>
. The weird think is that when i target the elements like this :
const btn1 = something1;
const btn2 = something2;
It gives me a different result from declaring it like this
const btn1 = something1, btn2 = something2;
What is going on with this? Do these two ways of the declaration have to do anything with the scope?
What would be preferable and under what circumstances should I use the one rather than the other?
Here is a pen for a quick review.
UPDATE
I also came across another weird 'issue'.
When I try to make a declaration of the two of the above variables like this
const /* hit enter here */
btn1 = something1, btn2 = something2
i get as a result the class vale of the button and not the inner <i class='...'/>
javascript javascript-events constants declaration
I have a very weird issue going on. I create 2 event listeners one for each button and the i just console.log(e.target.className)
The button have also inside them an
<i class='...'/>
. The weird think is that when i target the elements like this :
const btn1 = something1;
const btn2 = something2;
It gives me a different result from declaring it like this
const btn1 = something1, btn2 = something2;
What is going on with this? Do these two ways of the declaration have to do anything with the scope?
What would be preferable and under what circumstances should I use the one rather than the other?
Here is a pen for a quick review.
UPDATE
I also came across another weird 'issue'.
When I try to make a declaration of the two of the above variables like this
const /* hit enter here */
btn1 = something1, btn2 = something2
i get as a result the class vale of the button and not the inner <i class='...'/>
javascript javascript-events constants declaration
javascript javascript-events constants declaration
edited Nov 13 '18 at 13:53
Evan
asked Nov 13 '18 at 13:43
EvanEvan
88
88
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
Ah, I see what's happening, this is nothing to do with theconst
, it's how events propagate,. A listener will attach to all siblings,<button><i>
so it depends on were you click the button, if you hit thei
tag, or thebutton
tag.. Try usingcurrentTarget
this is the element you attached the event listener onto.
– Keith
Nov 13 '18 at 13:56
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
If you want the btn, it's justcurrentTarget.className
, if you want the<i>
,currentTarget.querySelector("i").className
yes, should have said child elements,..
– Keith
Nov 13 '18 at 14:03
|
show 1 more comment
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
Ah, I see what's happening, this is nothing to do with theconst
, it's how events propagate,. A listener will attach to all siblings,<button><i>
so it depends on were you click the button, if you hit thei
tag, or thebutton
tag.. Try usingcurrentTarget
this is the element you attached the event listener onto.
– Keith
Nov 13 '18 at 13:56
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
If you want the btn, it's justcurrentTarget.className
, if you want the<i>
,currentTarget.querySelector("i").className
yes, should have said child elements,..
– Keith
Nov 13 '18 at 14:03
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
Ah, I see what's happening, this is nothing to do with the
const
, it's how events propagate,. A listener will attach to all siblings, <button><i>
so it depends on were you click the button, if you hit the i
tag, or the button
tag.. Try using currentTarget
this is the element you attached the event listener onto.– Keith
Nov 13 '18 at 13:56
Ah, I see what's happening, this is nothing to do with the
const
, it's how events propagate,. A listener will attach to all siblings, <button><i>
so it depends on were you click the button, if you hit the i
tag, or the button
tag.. Try using currentTarget
this is the element you attached the event listener onto.– Keith
Nov 13 '18 at 13:56
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
If you want the btn, it's just
currentTarget.className
, if you want the <i>
, currentTarget.querySelector("i").className
yes, should have said child elements,..– Keith
Nov 13 '18 at 14:03
If you want the btn, it's just
currentTarget.className
, if you want the <i>
, currentTarget.querySelector("i").className
yes, should have said child elements,..– Keith
Nov 13 '18 at 14:03
|
show 1 more comment
0
active
oldest
votes
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%2f53282379%2fwhy-i-get-different-output-values-with-the-same-declared-constants-on-an-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53282379%2fwhy-i-get-different-output-values-with-the-same-declared-constants-on-an-event%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
In your pen, the second way in the comments looks identical to the first, what am I missing?
– Keith
Nov 13 '18 at 13:53
I just updated @Keith..
– Evan
Nov 13 '18 at 13:55
Ah, I see what's happening, this is nothing to do with the
const
, it's how events propagate,. A listener will attach to all siblings,<button><i>
so it depends on were you click the button, if you hit thei
tag, or thebutton
tag.. Try usingcurrentTarget
this is the element you attached the event listener onto.– Keith
Nov 13 '18 at 13:56
I see. So how can we get only the add__btn class when hitting that button? Because of I target it right? And @Keith the <i class='...'/> is not an element sibling but an element child.
– Evan
Nov 13 '18 at 14:02
If you want the btn, it's just
currentTarget.className
, if you want the<i>
,currentTarget.querySelector("i").className
yes, should have said child elements,..– Keith
Nov 13 '18 at 14:03