As Quentin already explains in his answer, a document
object cannot be clicked upon.
Event handlers associated on document
execute due to the events bubbling up the DOM. For example, a click event on the button in your snippet traverses the button -> then body -> then document.
So, as an alternative to document.clicked()
you attempt in your question, you can trigger the event on body
element via, document.body.click()
.
/* Working fine */
function getAlert() {
alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()
/* Should be work */
document.onclick = getAlert;
document.body.click()
<button id="btn">Click Me!</button>
You can read more about event bubbling here: What is event bubbling and capturing?
Note: I have added ID attribute to the button in your snippet to ensure that it runs.