How to trigger ngClick programmatically

266,926

Solution 1

The syntax is the following:

function clickOnUpload() {
  $timeout(function() {
    angular.element('#myselector').triggerHandler('click');
  });
};

// Using Angular Extend
angular.extend($scope, {
  clickOnUpload: clickOnUpload
});

// OR Using scope directly
$scope.clickOnUpload = clickOnUpload;

More info on Angular Extend way here.

If you are using old versions of angular, you should use trigger instead of triggerHandler.

If you need to apply stop propagation you can use this method as follows:

<a id="myselector" ng-click="clickOnUpload(); $event.stopPropagation();">
  Something
</a>

Solution 2

angular.element(domElement).triggerHandler('click');

EDIT: It appears that you have to break out of the current $apply() cycle. One way to do this is using $timeout():

$timeout(function() {
    angular.element(domElement).triggerHandler('click');
}, 0);

See fiddle: http://jsfiddle.net/t34z7/

Solution 3

This following solution works for me :

angular.element(document.querySelector('#myselector')).click();

instead of :

angular.element('#myselector').triggerHandler('click');

Solution 4

Just in case everybody see's it, I added additional duplicating answer with an important line which will not break event propagation

$scope.clickOnUpload = function ($event) {    
    $event.stopPropagation(); // <-- this is important

    $timeout(function() {
        angular.element(domElement).trigger('click');
    }, 0);
};

Solution 5

Using plain old JavaScript worked for me:
document.querySelector('#elementName').click();

Share:
266,926

Related videos on Youtube

mulla.azzi
Author by

mulla.azzi

Updated on April 11, 2020

Comments

  • mulla.azzi
    mulla.azzi about 4 years

    I want to trigger ng-click of an element at runtime like:

    _ele.click();
    

    OR

    _ele.trigger('click', function());
    

    How can this be done?

    • mulla.azzi
      mulla.azzi about 10 years
      No, i want to know the mechanism through which i can trigger ng-click manually.
  • mulla.azzi
    mulla.azzi about 10 years
    even this is nor working, its throwing following error Error: [$rootScope:inprog] errors.angularjs.org/1.2.14/$rootScope/inprog?p0=%24apply at Error (native)
  • Blago
    Blago about 10 years
    Can you replace the body of your click handler function with this line: alert('boom')? Can you see the alert?
  • Blago
    Blago about 10 years
    You are going to have to show us a test case. Please send a fiddle if you want further assistance.
  • mulla.azzi
    mulla.azzi about 10 years
    are you sure triggerHandler will fire ng-click event?
  • mulla.azzi
    mulla.azzi about 10 years
    jsfiddle.net/U3pVM/3668 here is a small fiddle. Click on third check box and FIRST check box should also get selected
  • WildlyInaccurate
    WildlyInaccurate almost 10 years
    You probably want to use Angular's $timeout rather than setTimeout, as $timeout will run an $apply cycle for you if necessary. It also makes your code more testable as ngMock gives you full control over when $timeouts are executed. docs.angularjs.org/api/ng/service/$timeout
  • Martin Probst
    Martin Probst almost 10 years
    Actually this should be $timeout(fn, 0, false); so that it doesn't invoke $apply, shouldn't it?
  • Blago
    Blago almost 10 years
    $timeout will break out of the current $apply cycle even with delay=0. So in the context of this question, the third argument is irrelevant.
  • Blago
    Blago almost 10 years
    what version of angular are you using. triggerHandler seems to work fine in 1.2.1: jsfiddle.net/t34z7
  • Howie
    Howie over 9 years
    This should be the accepted answer. triggerHandler works. trigger does not in v1.2.25
  • Ulterior
    Ulterior over 9 years
    dont forget to add '$event.stopPropagation();' just after the function start
  • Sergii Shevchyk
    Sergii Shevchyk about 9 years
    please explain why $(elem).click() doesn't work with Angular?
  • Hassan Faghihi
    Hassan Faghihi almost 9 years
    @WildlyInaccurate you sure $timeout is for angular and not jquery, cause when i call $timeout, it say "ReferenceError: $timeout is not defined" ... txtNickName: function () { $timeout(function () { angular.element('#btnStartChat').triggerHandler('click'); //angular.element('#btnStartChat').trigger('click'); }, 0);
  • WildlyInaccurate
    WildlyInaccurate almost 9 years
    @deadManN $timeout is a service and as such needs to be dependency-injected before you can use it docs.angularjs.org/api/ng/service/$timeout
  • Daniel Nalbach
    Daniel Nalbach almost 9 years
    If you are limited to jqLite and can't use a selector, do this instead: angular.element(document.querySelector('#mySelector')).trigg‌​er('click');
  • yorch
    yorch over 8 years
    @DanielNalbach in current (and not so current) Angular versions 1.2+, you have to use triggerHandler instead of trigger
  • Huangism
    Huangism over 8 years
    Yea .trigger() is not a function in jqLite
  • jpmottin
    jpmottin over 7 years
    @DotKu Maybe you are already in a $scope function like when you use in a $timeout function ? In other words, you can't call a $scope.$apply() in a $scope.$apply()... I hope this might helps you.
  • Weijing Jay Lin
    Weijing Jay Lin over 7 years
    @jpmottin I found it, it must be placed in $timeout. Thanks
  • trysis
    trysis almost 7 years
    Why did they remove trigger? It seems better/easier than triggerHandler. For one thing, triggerHandler does not even call the default browser click event, only the handlers you already attached.
  • Florida G.
    Florida G. almost 7 years
    Thanks! This finally worked for me in 1.5.11, and full jquery dependency. $timeout(function() { var el = document.getElementsByClassName('fa-chevron-up'); angular.element(el).trigger('click'); }, 0);
  • Jhourlad Estrella
    Jhourlad Estrella almost 7 years
    OMG, Angular totallt ruined it. Why can't they mimick jQuery's $('#element').click()
  • Abhishek
    Abhishek over 6 years
    I'm not so sure this still works...angularjs 1.6 this seems to not be working with full jquery
  • ktaro
    ktaro about 5 years
    Yeah. Works for me. Thanks.
  • Todd Hale
    Todd Hale about 4 years
    I get this error for this solution in my ng 1.5.3 app: Looking up elements via selectors is not supported by jqLite.