112

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

_ele.click();

OR

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

How can this be done?

isherwood
  • 58,414
  • 16
  • 114
  • 157
mulla.azzi
  • 2,676
  • 4
  • 18
  • 25

10 Answers10

189

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>
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
clopez
  • 4,372
  • 3
  • 28
  • 42
  • 4
    what version of angular are you using. triggerHandler seems to work fine in 1.2.1: http://jsfiddle.net/t34z7/ – Blago Jul 18 '14 at 18:12
  • 14
    please explain why $(elem).click() doesn't work with Angular? – Sergii Shevchyk Mar 25 '15 at 12:18
  • 13
    If you are limited to jqLite and can't use a selector, do this instead: angular.element(document.querySelector('#mySelector')).trigger('click'); – Daniel Nalbach Jul 06 '15 at 23:08
  • 3
    @DanielNalbach in current (and not so current) Angular versions 1.2+, you have to use `triggerHandler` instead of `trigger` – yorch Oct 24 '15 at 08:51
  • Yea `.trigger()` is not a function in jqLite – Huangism Oct 26 '15 at 17:11
  • 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. – trysis Jun 15 '17 at 16:53
  • 1
    I'm not so sure this still works...angularjs 1.6 this seems to not be working with full jquery – George Mauer Jan 02 '18 at 03:55
83
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/

Blago
  • 4,697
  • 2
  • 34
  • 29
  • 2
    even this is nor working, its throwing following error Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.14/$rootScope/inprog?p0=%24apply at Error (native) – mulla.azzi Mar 17 '14 at 05:27
  • Can you replace the body of your click handler function with this line: alert('boom')? Can you see the alert? – Blago Mar 17 '14 at 05:31
  • You are going to have to show us a test case. Please send a fiddle if you want further assistance. – Blago Mar 17 '14 at 05:34
  • are you sure triggerHandler will fire ng-click event? – mulla.azzi Mar 17 '14 at 05:35
  • http://jsfiddle.net/U3pVM/3668/ here is a small fiddle. Click on third check box and FIRST check box should also get selected – mulla.azzi Mar 17 '14 at 05:51
  • 9
    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 `$timeout`s are executed. https://docs.angularjs.org/api/ng/service/$timeout – WildlyInaccurate Jun 03 '14 at 10:13
  • 2
    Actually this should be `$timeout(fn, 0, false);` so that it doesn't invoke $apply, shouldn't it? – Martin Probst Jun 24 '14 at 21:37
  • $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 Jun 25 '14 at 02:07
  • 2
    This should be the accepted answer. triggerHandler works. trigger does not in v1.2.25 – Howie Oct 03 '14 at 10:28
  • dont forget to add '$event.stopPropagation();' just after the function start – Ulterior Dec 01 '14 at 08:34
  • @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);` – Hassan Faghihi Jun 17 '15 at 14:12
  • 4
    @deadManN `$timeout` is a service and as such needs to be dependency-injected before you can use it https://docs.angularjs.org/api/ng/service/$timeout – WildlyInaccurate Jun 19 '15 at 08:23
20

This following solution works for me :

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

instead of :

angular.element('#myselector').triggerHandler('click');
jpmottin
  • 2,717
  • 28
  • 25
13

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);
};
Ulterior
  • 2,786
  • 3
  • 30
  • 58
  • 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); – Florida G. Jun 25 '17 at 15:17
10

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

charlchad
  • 3,381
  • 1
  • 16
  • 9
6

The best solution is to use:

domElement.click()

Because the angularjs triggerHandler(angular.element(domElement).triggerHandler('click')) click events does not bubble up in the DOM hierarchy, but the one above does - just like a normal mouse click.

https://docs.angularjs.org/api/ng/function/angular.element

http://api.jquery.com/triggerhandler/

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85
4

You can do like

$timeout(function() {
   angular.element('#btn2').triggerHandler('click');
});
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • 1
    I get this error for this solution in my ng 1.5.3 app: Looking up elements via selectors is not supported by jqLite. – Todd Hale Mar 10 '20 at 00:35
1

This code will not work (throw an error when clicked):

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

You need to use the querySelector as follows:

$timeout(function() {
   angular.element(document.querySelector('#btn2')).triggerHandler('click');
});
1

Simple sample:

HTML

<div id='player'>
    <div id="my-button" ng-click="someFuntion()">Someone</div>
</div>

JavaScript

$timeout(function() {
    angular.element('#my-button').triggerHandler('click');
}, 0);

What this does is look for the button's id and perform a click action. Voila.

Source: https://techiedan.com/angularjs-how-to-trigger-click/

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

Include following line in your method there you want to trigger click event

angular.element('#btn_you_want_to_click_progmaticallt').triggerHandler('click');
});
Vinayak Shedgeri
  • 2,222
  • 1
  • 21
  • 24