Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.click() on a document

DOM button is getting clicked with .click() method, but its not working on document itself.

/* Working fine */
function getAlert() {
    alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()

/* Should be work  */
document.onclick = getAlert;
document.clicked()
<button id="btn">Click Me!</button>

Please help.

like image 682
Sudhakar Lahane Avatar asked Sep 22 '26 00:09

Sudhakar Lahane


1 Answers

document objects are not things that can be clicked on.

Event handlers can be bound to a document, and events will bubble until they reach the document and trigger a handler, but you can't click directly on the document.

like image 116
Quentin Avatar answered Sep 24 '26 13:09

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!