6

how to migrate the below function of jquery to angular js ?

  $( "#foo" ).trigger( "click" );

Problem here is im planning to trigger submit button automatically when user fills some data in our application.

so as im from jquery background ,

thanks in advance.

1
  • 3
    Please consider adding a comment when voting down so that the question can be improved. Thanks. Commented Aug 13, 2014 at 12:14

5 Answers 5

16
$scope.triggerClick = function () {
  $timeout(function() {
    angular.element('#foo').trigger('click');
  }, 100);
};

The $timeout will run an $apply to the cycle if necessary.

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

3 Comments

I have given this a +1 because it is a factually accurate answer to the question.
"Looking up elements via selectors is not supported by jqLite". So angular.element(document.getElementById('#foo')) and "trigger() is not a function" so .triggerHandler('click')
I guess the question has tag jquery that is why it's voted as correct answer.
9
$timeout(function() {
    angular.element(domElement).triggerHandler('click');
}, 0);

The $timeout is for breaking angular's $apply cycle.

2 Comments

}, 0); is the same as omitting the duration, like so... });
does this count as domElement: $('.filter-tags-container .tag')
1

Usually you don't submit a form in AngularJS. You send the data using an XHR and get a response in JSON.

Something like this:

VIEW

<form name="myForm" ng-submit="login(credentials)">

  <label>
    Username:
    <input type="text" ng-model="credentials.username" />
  </label>

  <label>
    Password:
    <input type="password" ng-model="credentials.password" />
  </label>

 <button type="submit">Login</button>

</form>

CONTROLLER

$scope.credentials = {};
$scope.login = function login(credentials) {
  var user = credentials.username;
  var pass = credentials.password;

  // Do some data validation
  if (!user || !pass) {
    $window.alert('Please, enter a username and a password!');
    return;
  }

  // Send the data and parse the response
  // (usually done via a Service)
  LoginService.login(user, pass);
};

See, also, this short demo.

2 Comments

This is a simple and clean form example with AngularJS but I just can't see how is this right answer to the question...
@s.alem: If you read the question carefully you will see why :) The question is about migrating a programmatic form submission from jQuery to Angular.
-1

Instead of that, you could use ng-change and call the submit function from your controller. Something like this:

<input type="text" ng-model="userData.field1" ng-change="mySubmitFunction(userData)">

4 Comments

Why on earth would someone want to submit a form on an text-field's change event ???
Maybe some kind of typeahead?
In that case, you don't submit any form; you make an XHR request. There is a huge difference. The question is about submitting a form.
You're right. It has been so long since I submit anything without ajax so I didn't even think about it...
-2

If you f.eks have a button you need to use ng-click="myFunctionName()" on the button itself.

And in the script file you use myFunctionName = $scope.function(){ yourCode... }

If you care completely new to Angular... you should read up a bit on it, since it basically stays away from the DOM, takes "control" over your webpage, and needs ng-app, ng-controller and uses $scope to hold states for content and data.

1 Comment

This means... you do not use classnames for identificators anymore... you use ex : <a ng-click="myFunctionName()" class="usedOnlyForStyling" href="handleInNgClickEvent". Remember you need a ng-app and a ng-controller too :D Hope this helps mate !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.