Function in ng-click gets called on keypress(Enter key)

1,222 views
Skip to first unread message

Satish Pabla

unread,
Feb 4, 2016, 10:06:27 AM2/4/16
to AngularJS
Hi All,

 I have following code:

 <a href="javascript:void(0)" ng-click="setActive($event,question)" ng-keypress="setActive($event,question)" tabindex="3">

and the function in controller is
        if( event.type == 'click' || ( event.type == 'keypress' && event.keyCode == '13' ) )
        {
                   question.isActive = !question.isActive;   
        }

The issue is, when I hit enter key then both the events i.e click event and keypress event gets called. This is working fine on mouse click.

Is this issue due to ng-keypress and ng-click on same element?

Please help!!


Bill Boyle

unread,
Feb 4, 2016, 2:49:23 PM2/4/16
to AngularJS
I tried to re-create this and it appears to work fine for me:


You might have an issue somewhere else in your code, but its probably not because of ng-keypress and ng-click on the same element.

Bill

Sander Elias

unread,
Feb 10, 2016, 3:22:11 AM2/10/16
to AngularJS
Hi Satish,

You problem is in your browser. The browser will execute the click event on the activate keyboard command (in 98% of all cases, press enter on a link will do that, but there are a few exceptions) Also there are some differences between browsers in this area too.
So, yes, the problem is in having both on your element. You have to handle that case manually. Simplest way is to debounce your function a bit.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages