jQuery: how to add eventListener to whole document/Webpage

Viewed 53

How do I add an eventListener to whole document with jQuery?

$("document").click(function (){        
  $("h1").css('color', 'red');
})

I want the h1 to change color if clicked anywhere in the document/webpage. Thanks!

2 Answers

Just remove the double quotations in the $("document") line

$(document).click(function (){ 
  $("h1").css('color', 'red');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Hello</h1>

just using "body" in quotation marks as following also works.

$("body").click(function (){ 
  $("h1").css('color', 'red');
})
Related