0

I would like to force the cursor to go to an input field immediately after the webpage is loaded. I am using AngularJS and the HTML code for the input field is as follows;

  <form novalidate="" class="simple-form">Identity number 
  <input type="text" ng-model="input_number" />
  <br />
  <button ng-click="submit()">SUBMIT</button></form>

How can this be done the AngularJS way? If not, using normal javascript or jquery are also welcomed.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • You might find this useful: http://stackoverflow.com/questions/17606936/angularjs-dot-in-ng-model – Cybrix Apr 23 '14 at 02:49

3 Answers3

2

To complete @Felix answer, jQuery function should be wrapped inside a directive

yourapp.directive('autoFocus', function() {
  return {
    restrict: 'A',
    link: function($scope, $element, attrs) {
      $element.focus();
    }
  };
});

And you can use the directive like this <input type="text" auto-focus>

Also some good reading: https://docs.angularjs.org/guide/directive and http://www.sitepoint.com/practical-guide-angularjs-directives/

Cybrix
  • 3,248
  • 5
  • 42
  • 61
2

You can also use simply autofocus html5 attribute.But it is not supported in Internet Explorer 9 and earlier versions.

Arkar Aung
  • 3,554
  • 1
  • 16
  • 25
0

You can use .focus() with jQuery:

$('form input[type=text]:eq(0)').focus();
Felix
  • 37,892
  • 8
  • 43
  • 55