How can I run a JavaScript function with any click on the page?

html, javascript

Solution

You can attach onclick event on document. jsfiddle

​document.onclick = function(){
 // your code
 //alert("clicked");
}​

If you want change on focus then use

window.onfocus = function(){

   // your code
     //alert("focus");
}

Problem

Right now I'm using `<body onload="function">`, which changes some text on the page based on which element is focused on. It works fine, but I need my site to run the function every time the focus changes (or, even better, every time any part of the page is clicked). Currently the code looks like this: ``` <body onload="changeText()"> <script> function changeText(){ function here; } </script> <p>This is where text changes based on the function</p> ``` Thanks!

Original source

Related problems