1

Im trying to call ng-click from jQuery but cant get it work. Can someone tell me how to do it properly?

HTML:

<a ng-click="changeData()">Testing</a>
<a ng-click="changeData()">Testing 2</a>
<a ng-click="changeData()">Testing 3</a>

jQuery:

$(document).keydown(function(e){
    if (checkNav && checkNav()) return;
    if (e.keyCode == 37) {
        // left
        setCurrent(x,y-1);
        e.preventDefault();
    } else if (e.keyCode == 38) {
        // up
        setCurrent(x-1,y);
        e.preventDefault();
    } else if (e.keyCode == 39) {
        // right
        setCurrent(x,y+1);
        e.preventDefault();
    } else if (e.keyCode == 40) {
        // down
        setCurrent(x+1,y);
        e.preventDefault();
    } else if (e.keyCode == 13) {

        $('a[ng-click="changeData()"]')

    }
});

I want it to call same function as ng-click when clicking enter. It works fine if I have the following code for hrefs when clicking enter:

} else if (e.keyCode == 13) {
                window.location = current.attr('href');
                e.preventDefault();
            }

Thanks!

4
  • ng-keyup Is something like this what you're looking for? Commented Jul 8, 2015 at 21:51
  • Thanks for the reply! I tried but couldnt get it to work. Basically when I click "enter" key, I want the same function to be triggered that's being triggered by ng-click. Do you have some idea what to do perhaps? Commented Jul 8, 2015 at 22:01
  • maybe add a scope function and ng-keydown="keyDown($event)" .. the function would look like this $scope.keyDown = function(e) { if(e.keyCode === 13) {e.preventDefault(); $scope.changeData(); } }); Commented Jul 8, 2015 at 22:15
  • Thanks for the reply! I need it for an experimental feature. I tried with ng-click="changeData()" but it didn't work either. I didnt get any error in the console either, strange. Commented Jul 8, 2015 at 22:35

2 Answers 2

1

I think you mean you want something like this. Let me know if this is your intent or not:

index.html

<input ng-keyup="keyFn(event=$event)" ngclick="clickFn()">click</input>

controller.js

angular.module('myApp')
    .controller('myCtrl', function($scope) {

        $scope.keyFn = function(event){
          if (event.keyCode && event.keyCode === 13/*enter key*/){
              someFunction();
          }
        };
        $scope.clickFn = function(){
            someFunction();
        }
    });

function someFunction(){
    console.log("event fired");
    //functionality shared between click and `enter` keypress
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to do this on something else than input field? Like button or href when its focused? Right now it works fine on input field but not something else. Thanks!
ng-focus="clickFn()"
Thanks Jan! However, when I click on "tab", the focus doesnt change to the input submit. Do you happen to know how I can achieve this?
These are all things you could have found yourself with a quick google search. F.e "angular bind focus" and "angular trigger focus"
0

$('a[ng-click="changeData()"]') just fetches the element. If you want to trigger a click you need to call .click() like so

$('a[ng-click="changeData()"]').click();

But I prefer thomas' answer that doesn't use jQuery at all, I don't see why you'd need it. Seems completely unnecessary for this.

1 Comment

Thanks for the reply! I need it for an experimental feature. I tried with ng-click="changeData()" but it didn't work either. I didnt get any error in the console either, strange.

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.