Open In App

How to click anywhere on the page except one element using jQuery ?

A web-page contains many elements and the task is to click anywhere on the page except one element using jQuery. There are two methods to solve this problem which are discussed below:

Approach 1:



Example: This example implements the above approach. 




<head>
    <script src=
    </script>
      
    <style>
        body {
            height: auto;
        }
        #t {
            height: 100px;
            width: 350px;
            text-align:justify;
        }
    </style>
</head>
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
      
    <p id="GFG_UP">
    </p>
      
    <textarea id="t">
            jQuery is an open source JavaScript library
            that simplifies the interactions between an
            HTML/CSS document, or more precisely the
            Document Object Model (DOM), and JavaScript.
            Elaborating the terms, jQuery simplifies
            HTML document traversing and manipulation,
            browser event handling, DOM animations,
            Ajax interactions, and cross-browser
            JavaScript development.
        </textarea>
    <br>
      
    <button onclick="gfg_Run()">
        click here
    </button>
      
    <p id="GFG_DOWN" style="color:green;
            font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Click anywhere on the body "
                    + "except textarea to see effect.";
          
        $('body').click(function(event) {
              
            if(event.target.id == "t")
                return;
            if($(event.target).closest('t').length)
                return;
              
            el_down.innerHTML = "Clicked on the "
                        + "body except textarea.";
        });
    </script>
</body>

Output:



How to click anywhere on the page except one element using jQuery ?

Approach 2:

Example: This example implements the above approach. 




<head>
    <script src=
    </script>
</head>
<body>
    <style>
        body {
            height: auto;
        }
        #t {
            height: 100px;
            width: 350px;
            text-align:justify;
        }
    </style>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
      
    <p id="GFG_UP">
    </p>
      
    <textarea id="t">
            jQuery is an open source JavaScript library
            that simplifies the interactions between an
            HTML/CSS document, or more precisely the
            Document Object Model (DOM), and JavaScript.
            Elaborating the terms, jQuery simplifies
            HTML document traversing and manipulation,
            browser event handling, DOM animations,
            Ajax interactions, and cross-browser
            JavaScript development.
        </textarea>
    <br>
      
    <button onclick="gfg_Run()">
        click here
    </button>
      
    <p id="GFG_DOWN" style="color:green; 
                                                 font-size: 20px; 
                                                 font-weight: bold;">
    </p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Click anywhere on the body"
                    + " except textarea to see effect.";
          
        $('html').click(function() {
            el_down.innerHTML = "Clicked on the body"
                        + " except textarea.";
        });
          
        $('#t').click(function(event) {
            event.stopPropagation();
        });
    </script>
</body>

Output:

How to click anywhere on the page except one element using jQuery ?


Article Tags :