Weird behaviour with on click event binding; event not firing
So I came across this weird issue and I don't know if I'm blatantly missing something. Visit this page (or any medium article). Open console and inject the following JS code.
for (const elem of document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image'))
elem.addEventListener('click', function ()
console.log(event.target);
);
Now click on the big images, expected behaviour should be (please correct me cause I seem to be wrong) that the target element is printed when you click on it the first time and also the second time. But as it turns out when you click on the image (zoomed) the second time (to zoom out) it doesn't print the target in the console.
I thought that there might be some overlay element and hence I bind the event on body
to capture all of the events using the following injected JS.
document.body.addEventListener('click', function ()
console.log(event.target);
, true);
But even with that I only get one console print of the target.
One theory for delegation using body
not working might be following -
The newly created element would not be in the body
in its time of creation, it will be moved to its place in the DOM tree later on. And hence delegation is not able to find it when did via body
but able to capture it via document
.
After a bit more exploring and injecting the following JS (taken from here and I know break point can be added, I did do that earlier but to no end so resorted to this.)
var observer = new MutationObserver(function (mutationsList, observer)
for (var mutation of mutationsList)
if (mutation.type == 'childList')
console.log('A child node has been added or removed.');
else if (mutation.type == 'attributes')
console.log('The ' + mutation.attributeName + ' attribute was modified.');
);
observer.observe(document,
attributes: true,
childList: true,
subtree: true
);
I don't see any element being added to the DOM on click (it is being added on load) so that theory might not be correct. So I guess now the real question is why Event Capturing through document
is able to find the click event where else not from body
. I don't think delegation works on initial DOM structure since it would break the purpose.
Also if it is a duplicate please let me know, since I don't really know what to exactly search for.
javascript html debugging
add a comment |
So I came across this weird issue and I don't know if I'm blatantly missing something. Visit this page (or any medium article). Open console and inject the following JS code.
for (const elem of document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image'))
elem.addEventListener('click', function ()
console.log(event.target);
);
Now click on the big images, expected behaviour should be (please correct me cause I seem to be wrong) that the target element is printed when you click on it the first time and also the second time. But as it turns out when you click on the image (zoomed) the second time (to zoom out) it doesn't print the target in the console.
I thought that there might be some overlay element and hence I bind the event on body
to capture all of the events using the following injected JS.
document.body.addEventListener('click', function ()
console.log(event.target);
, true);
But even with that I only get one console print of the target.
One theory for delegation using body
not working might be following -
The newly created element would not be in the body
in its time of creation, it will be moved to its place in the DOM tree later on. And hence delegation is not able to find it when did via body
but able to capture it via document
.
After a bit more exploring and injecting the following JS (taken from here and I know break point can be added, I did do that earlier but to no end so resorted to this.)
var observer = new MutationObserver(function (mutationsList, observer)
for (var mutation of mutationsList)
if (mutation.type == 'childList')
console.log('A child node has been added or removed.');
else if (mutation.type == 'attributes')
console.log('The ' + mutation.attributeName + ' attribute was modified.');
);
observer.observe(document,
attributes: true,
childList: true,
subtree: true
);
I don't see any element being added to the DOM on click (it is being added on load) so that theory might not be correct. So I guess now the real question is why Event Capturing through document
is able to find the click event where else not from body
. I don't think delegation works on initial DOM structure since it would break the purpose.
Also if it is a duplicate please let me know, since I don't really know what to exactly search for.
javascript html debugging
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13
add a comment |
So I came across this weird issue and I don't know if I'm blatantly missing something. Visit this page (or any medium article). Open console and inject the following JS code.
for (const elem of document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image'))
elem.addEventListener('click', function ()
console.log(event.target);
);
Now click on the big images, expected behaviour should be (please correct me cause I seem to be wrong) that the target element is printed when you click on it the first time and also the second time. But as it turns out when you click on the image (zoomed) the second time (to zoom out) it doesn't print the target in the console.
I thought that there might be some overlay element and hence I bind the event on body
to capture all of the events using the following injected JS.
document.body.addEventListener('click', function ()
console.log(event.target);
, true);
But even with that I only get one console print of the target.
One theory for delegation using body
not working might be following -
The newly created element would not be in the body
in its time of creation, it will be moved to its place in the DOM tree later on. And hence delegation is not able to find it when did via body
but able to capture it via document
.
After a bit more exploring and injecting the following JS (taken from here and I know break point can be added, I did do that earlier but to no end so resorted to this.)
var observer = new MutationObserver(function (mutationsList, observer)
for (var mutation of mutationsList)
if (mutation.type == 'childList')
console.log('A child node has been added or removed.');
else if (mutation.type == 'attributes')
console.log('The ' + mutation.attributeName + ' attribute was modified.');
);
observer.observe(document,
attributes: true,
childList: true,
subtree: true
);
I don't see any element being added to the DOM on click (it is being added on load) so that theory might not be correct. So I guess now the real question is why Event Capturing through document
is able to find the click event where else not from body
. I don't think delegation works on initial DOM structure since it would break the purpose.
Also if it is a duplicate please let me know, since I don't really know what to exactly search for.
javascript html debugging
So I came across this weird issue and I don't know if I'm blatantly missing something. Visit this page (or any medium article). Open console and inject the following JS code.
for (const elem of document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image'))
elem.addEventListener('click', function ()
console.log(event.target);
);
Now click on the big images, expected behaviour should be (please correct me cause I seem to be wrong) that the target element is printed when you click on it the first time and also the second time. But as it turns out when you click on the image (zoomed) the second time (to zoom out) it doesn't print the target in the console.
I thought that there might be some overlay element and hence I bind the event on body
to capture all of the events using the following injected JS.
document.body.addEventListener('click', function ()
console.log(event.target);
, true);
But even with that I only get one console print of the target.
One theory for delegation using body
not working might be following -
The newly created element would not be in the body
in its time of creation, it will be moved to its place in the DOM tree later on. And hence delegation is not able to find it when did via body
but able to capture it via document
.
After a bit more exploring and injecting the following JS (taken from here and I know break point can be added, I did do that earlier but to no end so resorted to this.)
var observer = new MutationObserver(function (mutationsList, observer)
for (var mutation of mutationsList)
if (mutation.type == 'childList')
console.log('A child node has been added or removed.');
else if (mutation.type == 'attributes')
console.log('The ' + mutation.attributeName + ' attribute was modified.');
);
observer.observe(document,
attributes: true,
childList: true,
subtree: true
);
I don't see any element being added to the DOM on click (it is being added on load) so that theory might not be correct. So I guess now the real question is why Event Capturing through document
is able to find the click event where else not from body
. I don't think delegation works on initial DOM structure since it would break the purpose.
Also if it is a duplicate please let me know, since I don't really know what to exactly search for.
javascript html debugging
javascript html debugging
edited Nov 15 '18 at 17:03
Divya Mamgai
asked Nov 15 '18 at 15:10
Divya MamgaiDivya Mamgai
1039
1039
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13
add a comment |
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13
add a comment |
3 Answers
3
active
oldest
votes
probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation.
I've got success with this
document.addEventListener("click", function(e) console.log(e.target); , true);
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
add a comment |
The image (you are trying to target) is dynamically made. After you already clicked the image once you should be able to target it.
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
add a comment |
document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')
This queries the DOM for all elements that have both the class progressiveMedia-image
and js-progressiveMedia-image
. You iterate over the result and bind an event listener to the click
event of each element.
When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. These new elements might have the same classes, but did not exist originally when you searched the DOM. As such, they do not have their click
event bound.
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
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%2f53322430%2fweird-behaviour-with-on-click-event-binding-event-not-firing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation.
I've got success with this
document.addEventListener("click", function(e) console.log(e.target); , true);
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
add a comment |
probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation.
I've got success with this
document.addEventListener("click", function(e) console.log(e.target); , true);
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
add a comment |
probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation.
I've got success with this
document.addEventListener("click", function(e) console.log(e.target); , true);
probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation.
I've got success with this
document.addEventListener("click", function(e) console.log(e.target); , true);
answered Nov 15 '18 at 15:26
lviggianilviggiani
3,24553660
3,24553660
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
add a comment |
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Why did the document.body not work then?
– Divya Mamgai
Nov 15 '18 at 15:59
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
Okay, I think delegation with body didn't work because the newly created element would not be in the body yet, it will be moved later on. And hence delegation is not able to find it when did via body.
– Divya Mamgai
Nov 15 '18 at 16:35
add a comment |
The image (you are trying to target) is dynamically made. After you already clicked the image once you should be able to target it.
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
add a comment |
The image (you are trying to target) is dynamically made. After you already clicked the image once you should be able to target it.
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
add a comment |
The image (you are trying to target) is dynamically made. After you already clicked the image once you should be able to target it.
The image (you are trying to target) is dynamically made. After you already clicked the image once you should be able to target it.
answered Nov 15 '18 at 15:25
GenericUserGenericUser
111115
111115
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
add a comment |
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
I completely missed that, feeling like a noob. But I did delegated the event using body even that didn't work.
– Divya Mamgai
Nov 15 '18 at 15:59
add a comment |
document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')
This queries the DOM for all elements that have both the class progressiveMedia-image
and js-progressiveMedia-image
. You iterate over the result and bind an event listener to the click
event of each element.
When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. These new elements might have the same classes, but did not exist originally when you searched the DOM. As such, they do not have their click
event bound.
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
add a comment |
document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')
This queries the DOM for all elements that have both the class progressiveMedia-image
and js-progressiveMedia-image
. You iterate over the result and bind an event listener to the click
event of each element.
When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. These new elements might have the same classes, but did not exist originally when you searched the DOM. As such, they do not have their click
event bound.
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
add a comment |
document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')
This queries the DOM for all elements that have both the class progressiveMedia-image
and js-progressiveMedia-image
. You iterate over the result and bind an event listener to the click
event of each element.
When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. These new elements might have the same classes, but did not exist originally when you searched the DOM. As such, they do not have their click
event bound.
document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')
This queries the DOM for all elements that have both the class progressiveMedia-image
and js-progressiveMedia-image
. You iterate over the result and bind an event listener to the click
event of each element.
When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. These new elements might have the same classes, but did not exist originally when you searched the DOM. As such, they do not have their click
event bound.
answered Nov 15 '18 at 15:27
PadaromPadarom
1,91732043
1,91732043
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
add a comment |
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
I did delegated the event using body even that didn't work. Any clues as to why?
– Divya Mamgai
Nov 15 '18 at 16:00
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%2f53322430%2fweird-behaviour-with-on-click-event-binding-event-not-firing%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
Can you add the link to the page?
– GenericUser
Nov 15 '18 at 15:12
@user20 Done, sorry for the delay.
– Divya Mamgai
Nov 15 '18 at 15:13