Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all page elements not to react on clicks

I'm trying to make an extension with 'pick' functionality: letting the user click on any element on the page and see the xpath for it.

However, to make it usable I have to prevent the element from reacting on clicking: I don't want clicking on hyperlinks to forward me to another page, buttons to submit forms, etc., etc.

How would I do that?

UPD: I need it only for Chrome

like image 938
lithuak Avatar asked Dec 13 '25 23:12

lithuak


2 Answers

The cleanest way in my opinion would be to bind one event handler to body in the capturing phase and prevent the event from propagating/the default behavior.

You can get a reference to the clicked element with event.target:

document.body.addEventListener('click', function(event) {
    event.preventDefault();
    event.stopPropagation();
    console.log(event.target);
}, true);

The advantage is that this event handler will be the very first that is triggered, no matter which other event handlers are bound to elements in the page (unless of course the page binds a similar event handler to body, but usually event handlers are bound in the bubbling phase).

For more information see quirksmode.org - Event order and MDN - addEventListener.

DEMO

like image 117
Felix Kling Avatar answered Dec 16 '25 12:12

Felix Kling


For a CSS only solution:

* {
    pointer-events: none;
}

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!