-1

can't for the life of me figure out why this doesn't work

document.querySelector('body').addEventListener('click', function () {
    console.log('wow')
});

I dont get any error, but nothing is logged onclick.

001
  • 13,291
  • 5
  • 35
  • 66
  • Works fine for me in a snippet-- are you sure that you're clicking on the body itself? It may only be as tall as the content it contains... – Alexander Nied Sep 21 '21 at 21:05
  • There are several things that could cause this issue. Absent html or other context, we can only assume which one is causing your problem. – Kevin B Sep 21 '21 at 21:20

1 Answers1

0

There is no content in the body, so it has a height of 0px. You can set its height:

document.querySelector('body').addEventListener('click', function() {
  console.log('wow')
});
html,
body {
  height: 100%;
}

Or attach the click event listener to the document instead:

document.addEventListener('click', function() {
  console.log('wow')
});
Spectric
  • 30,714
  • 6
  • 20
  • 43