Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:42:58+00:00 2026-06-12T23:42:58+00:00

Was wondering how I would implement a ‘Click everywhere but on this element’ event.

  • 0

Was wondering how I would implement a ‘Click everywhere but on this element’ event.

I have something that you can compare to a list of files in a file explorer. You can select certain elements but if you click outside the element controller it needs to deselect everything.

enter image description here

Added a screenshot to make it more clear. So what I want to do is that if I click anywhere but on the language elements it should fire an event.

Update

To clarify I am not asking how I can do this with jQuery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:43:00+00:00Added an answer on June 12, 2026 at 11:43 pm

    EDIT: There were a couple of problems in this old, old answer.

    *Also: Marking Community Wiki (no points for me) because errors

    1. N calls for N uses of the directive. This probably isn’t desirable for uses within the same scope with matching expressions.

    2. NOTHING WAS TEARING DOWN THE EVENT HANDLERS!!!! BAD! BAD! BAD!

    So, I’m updating this answer. Hopefully it didn’t cause anyone too much trouble.

    Updated answer

    Here’s a new plunker with those issues fixed … there are likely other things that individual application developers will run into. This is just an example of how to handle this problem.

    app.factory('clickAnywhereButHereService', function($document){
      var tracker = [];
      
      return function($scope, expr) {
        var i, t, len;
        for(i = 0, len = tracker.length; i < len; i++) {
          t = tracker[i];
          if(t.expr === expr && t.scope === $scope) {
            return t;    
          }
        }
        var handler = function() {
          $scope.$apply(expr);
        };
        
        $document.on('click', handler);
        
        // IMPORTANT! Tear down this event handler when the scope is destroyed.
        $scope.$on('$destroy', function(){
          $document.off('click', handler);
        });
        
        t = { scope: $scope, expr: expr };
        tracker.push(t);
        return t;
      };
    });
    
    app.directive('clickAnywhereButHere', function($document, clickAnywhereButHereService){
      return {
        restrict: 'A',
        link: function(scope, elem, attr, ctrl) {
          var handler = function(e) {
            e.stopPropagation();
          };
          elem.on('click', handler);
          
          scope.$on('$destroy', function(){
            elem.off('click', handler);
          });
          
          clickAnywhereButHereService(scope, attr.clickAnywhereButHere);
        }
      };
    });
    

    Original answer (with fixes for teardown of event handlers)

    You were close with the one answer you’ve found, but I’ve put together a plunk for you to show you what it was missing.

    app.directive('clickAnywhereButHere', function($document){
      return {
        restrict: 'A',
        link: function(scope, elem, attr, ctrl) {
          var elemClickHandler = function(e) {
            e.stopPropagation();
          };
          
          var docClickHandler = function() {
            scope.$apply(attr.clickAnywhereButHere);
          };
          
          elem.on('click', elemClickHandler);
          $document.on('click', docClickHandler);
          
          // teardown the event handlers when the scope is destroyed.
          scope.$on('$destroy', function() {
            elem.off('click', elemClickHandler);
            $document.off('click', docClickHandler);
          });
        }
      }
    })
    

    HTML

    <a click-anywhere-but-here="clickedSomewhereElse()" 
      ng-click="clickedHere()">Don't Click Me!</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering about how you would implement an effect similar to this one implemented
I'm wondering what would be best to do. Right now I have running a
So I am wondering what would cause it so that all events for the
I'm wondering how one would implement an outline view like the one Xcode 3
I was wondering which would be the most efficient approach to implement some kind
I was wondering what would be the best way to implement undirected graphs (and
I would like to implement something equivalent to the unix command 'rm foo*' using
Normally to set a boundary at a certain area, I would implement something like:
I was wondering how difficult it would be to implement Sproutcore into my webpage
I was wondering how request throttling would work with WebApi self-hosting? Would this be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.