4

In my app i'm using some logic after my element was clicked.

For example:

html

<h3 ng-click="showAlert('text')">this is text! (try to click and select)</h3>

js

$scope.showAlert = function(text) {
  console.log('click!');
};

and when i click on element: all is ok. All done, as was expected.

But!

Why, when i try to select text, my event is fired too?

how to skip select event?

enter image description here

plunker: click

3
  • There is no separate event for selection - so click will trigger anyway. You can make some checks on current selection, etc.etc. But I feel all this will hardly works good. Btw, clicking text is so unnatural behavior... Commented Nov 10, 2015 at 13:28
  • @PetrAveryanov for example toggling of editor (as jira do) Commented Nov 10, 2015 at 13:30
  • y, ok forgot about texteditables) Commented Nov 10, 2015 at 13:33

1 Answer 1

4

You can get the time between a mousedown and a mouseup to determine if it's a click or the text is being selected.

Like so: Demo

(function(){
  angular.module("app", []);

  angular.module("app")
  .directive("noclick", function(){
    return {
      restrict: "A",
      link: function(scope, element, attr){
        scope.time= Date.now();

        element.on("mousedown mouseup", function(e){
          if(e.type == "mousedown"){
            scope.time= Date.now();
          }

          if(e.type == "mouseup"){
            if(Date.now() - scope.time> 300){
              return false;
            }else{
              console.log("clicked");
            }
          }
        });
      }
    }
  });
})(window, angular);

Note: I'm using a directive for this case.

Sign up to request clarification or add additional context in comments.

9 Comments

i would set 100 msec, anyways +1
and if I press mouse button slowly -> no click ( may be u want to compare coordinates also... Smells bad anyway)
@MaximShoustin, Yes, I was using 300 in canvas game, 100 is better for this case though. Thanks.
@PetrAveryanov, how slowly?
This is really clean but a more robust version would be to check mouse movement instead of time. This way click works the way it does everywhere else.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.