0

I need help pressing the Enter key twice with a different action occurring after each keypress in AngularJS, for example:

  • Press Enter
  • One action happens
  • Press Enter again
  • A different action happens

I can get the first action working, just not the second. Here's my code:

  $scope.enterPressed = 0;
  $document.bind('keydown', function (evt) {
    if (evt.which === 13) {
      alert('Enter pressed');
      if ($scope.enterPressed === 0) {
        $scope.enterPressed++;
        document.getElementById("result").innerHTML = "Enter pressed once. $scope.enterPressed is " + $scope.enterPressed;
      } else if ($scope.enterPressed === 1) {
        e.preventDefault();
        document.getElementById("result").innerHTML = "Enter pressed twice. $scope.enterPressed i: " + $scope.enterPressed;
      }
    }
  });

Here's a Plunk.

How can I get the second action to occur?

2 Answers 2

2

You are using e.preventDefault(); but e is not defined

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

Comments

0

It should be

 evt.preventDefault();

Comments

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.