Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect any click on the page?

I want to catch the click event on any elements of the page. But I can not catch a click on a specific page elements. See example https://jsfiddle.net/rdnf5m1f/

 $(function() {
        $(".profile").on( "click","a", function() {
            console.log(1);
          return false;
        });

        // below not work
        $("body").on( "click","*", function() {
            console.log(2);
        });

        $("body").click(function() {
            console.log(2);
        });

        $(window).click(function() {
            console.log(2);
        });
 });

Why?

like image 450
Dmitriy Kupriyanov Avatar asked Oct 30 '25 20:10

Dmitriy Kupriyanov


1 Answers

Following what you need by "Capture all, Except some". Add a class 'nocapture' to the elements that you don't want to capture, and use the script:

jQuery('*:not(.nocapture)').on('click', function(e){
    e.stopPropagation();
    console.log('Element clicked', this); 
})
like image 140
Diego ZoracKy Avatar answered Nov 02 '25 08:11

Diego ZoracKy



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!