# Auto-focus Textbox while template loads

**URL:** <https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851>\
**Category:** ionic-v1\
**Created:** [July 15, 2014, 9:49am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851 "2014-07-15T09:49:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lomas](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@Lomas](https://forum.ionicframework.com/u/Lomas)\
**Post date:** [July 15, 2014, 9:49am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/1 "2014-07-15T09:49:12Z")

</div>

Hello All,  
I want to autofocus my textbox and keyboard should appear automatically while template loads as in the picture below.  
 ![](https://sea1.discourse-cdn.com/ionicframework/uploads/default/1220/ba427fa38177f50c.PNG)  
Any suggestions??

---

<div class="post-metadata">

**Author:** ![Lomas](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@Lomas](https://forum.ionicframework.com/u/Lomas)\
**Post date:** [July 29, 2014, 6:49am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/2 "2014-07-29T06:49:46Z")

</div>

hello Ionic geeks,  
why i am not getting answer to this?  
Is my question is not clear or there is not solution to this?

Thanks,  
Lomas Joshi

---

<div class="post-metadata">

**Author:** ![Hum](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@Hum](https://forum.ionicframework.com/u/Hum)\
**Post date:** [July 29, 2014, 10:19am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/3 "2014-07-29T10:19:51Z")

</div>

Hi Lomas,  
maybe I didn’t unterstand your question right but  
have you already tried to add `autofocus` to your input field?  
Like:

```
<input type="search" autofocus />

```

---

<div class="post-metadata">

**Author:** ![Lomas](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@Lomas](https://forum.ionicframework.com/u/Lomas)\
**Post date:** [July 29, 2014, 10:33am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/4 "2014-07-29T10:33:32Z")

</div>

Hello Hum,  
Thanks for the reply but unfortunately i already had used autofocus. it’s not working…  
I need textbox is focused and keyboard should be open(or shown) as in the picture. while app loads.

Thanks,  
Lomas Joshi

---

<div class="post-metadata">

**Author:** ![Hum](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@Hum](https://forum.ionicframework.com/u/Hum)\
**Post date:** [July 29, 2014, 4:30pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/5 "2014-07-29T16:30:19Z")

</div>

Hi Lomas,  
have you tried to put your input into the sub-header?

```
<ion-header-bar class="bar bar-subheader item-input-inset">
	<div class="item-input-wrapper">
		<i class="icon ion-ios7-search placeholder-icon"></i>
		<input type="search" autofocus />
		<button class="button button-icon ion-ios7-close-empty input-button"></button>
	</div>
</ion-header-bar>

```

I tried this and it worked.  
In the content it doesn’t work for me too.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 29, 2014, 4:38pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/6 "2014-07-29T16:38:45Z")

</div>

@Lomas, you can do this .

```
.directive('focusMe', function($timeout) {
  return {
    link: function(scope, element, attrs) {

      $timeout(function() {
        element[0].focus(); 
      });
    }
  };
}); 

```

Then add this to the config.xml

`<preference name="KeyboardDisplayRequiresUserAction" value="false" />`

> **[Tabs And Navigation: 1.0.0-beta.4](https://codepen.io/mhartington/pen/JAdkt)**
>
> Demo showing how each tab has its own history stack. Navigating between each tab remembers the correct back and forward views that were visited. Forke...

---

<div class="post-metadata">

**Author:** ![Lomas](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@Lomas](https://forum.ionicframework.com/u/Lomas)\
**Post date:** [July 30, 2014, 1:08pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/7 "2014-07-30T13:08:48Z")

</div>

Thanks Mhartingon, for your support…

Regards,  
Lomas Joshi

---

<div class="post-metadata">

**Author:** ![mainalikishan](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mainalikishan](https://forum.ionicframework.com/u/mainalikishan)\
**Post date:** [July 31, 2014, 4:56am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/9 "2014-07-31T04:56:37Z")

</div>

@mhartington : Sorry to say but didn’t worked as expected. Actually the auto focus and keyboard coming up worked but when keyboard is typed, input doesn’t show/display any text. Whats wrong now?

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 31, 2014, 1:05pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/10 "2014-07-31T13:05:11Z")

</div>

Hmm what version of ionic were you using? I just tested it myself and it works fine. Tested with beta 10 and the nightly builds

---

<div class="post-metadata">

**Author:** ![mainalikishan](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mainalikishan](https://forum.ionicframework.com/u/mainalikishan)\
**Post date:** [August 1, 2014, 3:46am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/11 "2014-08-01T03:46:00Z")

</div>

I am using beta 10 too. I have tested in IOS 7, and it is not working in right manner. Only if I touch the input area after keyboard is up; the typed text start displaying but when the page is first loaded and keyboard comes up; the typing only add whitespace to input area rather than text.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [August 1, 2014, 1:50pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/12 "2014-08-01T13:50:38Z")

</div>

Ah alright, I found the issue. It has to do how we set focus to the inputs.

Move the directive to be on the label wrapping it. This should resolve everything. Don’t forget to include that config.xml file.

```
<preference name="KeyboardDisplayRequiresUserAction" value="false" />
```

---

<div class="post-metadata">

**Author:** ![jammu](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jammu](https://forum.ionicframework.com/u/jammu)\
**Post date:** [August 21, 2014, 2:49am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/13 "2014-08-21T02:49:19Z")

</div>

Hi,  
I’m having the same issue. I’m using beta-11.

I put the autofocus directive on the lable and also set the config.xmll option.

When the view get’s rendered I want the Search field in the header get autofocus and the keyboard to open. However it’s not working.

Here is my HTML.  
Any tips would be greatly appreciated. I also tried creating a special directive. That didn’t work either.

```
<div class="bar bar-header has-footer item-input search-bar">
        <label class="item-input-wrapper" autofocus>
            <i class="icon ion-search placeholder-icon"></i>
            <input type="text" placeholder="Search"
                   autofocus
                   ng-model="searchKey">
        </label>
        <a class="button button-icon icon ion-ios7-close-outline"
           ng-click="clearSearch()">
        </a>
    </div>
```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [August 21, 2014, 2:54pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/14 "2014-08-21T14:54:34Z")

</div>

It’s not suggested to use to use html’s autofocus attribute. As it messes with our keyboard logic.

The directive is really the best way to go. You can add 150ms to the `$timeout` to make sure the view is completely rendered.

```auto
.directive('focusMe', function($timeout) {
  return {
    link: function(scope, element, attrs) {
      $timeout(function() {
        element[0].focus(); 
      }, 150);
    }
  };
});

```

Then use it like this

```auto
<label class="item-input-wrapper" focus-me >
  <i class="icon ion-search placeholder-icon"></i>
  <input type="text" placeholder="Search" ng-model="searchKey">
</label>

```

---

<div class="post-metadata">

**Author:** ![jammu](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jammu](https://forum.ionicframework.com/u/jammu)\
**Post date:** [August 28, 2014, 3:10pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/15 "2014-08-28T15:10:39Z")

</div>

Thanks. That was a huge help.  
It’s now working for me on iOS.

Couple of issues I’m seeing though:

One: On iOS, when I click on an item and go into a new nav-view, when I get back to the search view using the back button, the focus is happening automatically, even though I have logic in the directive to not focus. I tried blur but that didn’t make a different. Then I tried debugging the app but connecting Safari to the the phone, and connected, the problem doesn’t happen. Weird.  
Do you think it’s timing related?

Two: The autofocus logic that you suggested seems to work on iOS but it’s not working on Android. Do I need to do anything else on Android?

thanks again!

---

<div class="post-metadata">

**Author:** ![jammu](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jammu](https://forum.ionicframework.com/u/jammu)\
**Post date:** [September 3, 2014, 2:49am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/16 "2014-09-03T02:49:26Z")

</div>

Hi,

Any thoughts or ideas on the issue above?

thanks!

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [September 3, 2014, 2:59pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/17 "2014-09-03T14:59:10Z")

</div>

Hmm, whats the logic you have to prevent the focus the second time?

Just tested the directive on an android device and it works fine, at least the version I posted.

Again, could you post your version?

---

<div class="post-metadata">

**Author:** ![jammu](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jammu](https://forum.ionicframework.com/u/jammu)\
**Post date:** [September 3, 2014, 11:53pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/18 "2014-09-03T23:53:27Z")

</div>

Sure. This is my version:

scope.focusMe is set to true or false by the controller. it’s set to true if there are no search results to show on the page.  
That way the keyboard does not open. If there are no previous search results then it tells the directive to focus on the Search window and open the keyboard.

Re the android, issue. The focus is happening but the keyboard is not opening.

```
 .directive('focusMe', function($timeout) {
    return {
        link: function(scope, element, attrs) {
            console.log("focusMe directive init");
            if(scope.focusMe ) {
                $timeout(function() {
                    console.log(" adding focus to element")

                    element[0].focus();
                }, 150);
            }
        }
    };
})

Controller logic: if(Search.previousResults && Search.previousResults.length) {
            $scope.focusMe = false;
        } else {
            $scope.focusMe = true;
        }

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [September 5, 2014, 3:13pm UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/19 "2014-09-05T15:13:31Z")

</div>

Hm, want to throw together a codepen? I think I see what could be the issue, but I want to be sure by seeing the directive in your situation.

---

<div class="post-metadata">

**Author:** ![carthic333](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@carthic333](https://forum.ionicframework.com/u/carthic333)\
**Post date:** [December 1, 2014, 6:31am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/20 "2014-12-01T06:31:54Z")

</div>

@mhartington: i was looking for slimier directive for weeks, thanks man (y) .

@jammu : Try this

```
 .directive('focusMe', function($timeout) {
   return {
     scope: {
        focusValue: "=focusMe"
    },
        link: function(scope, element, attrs) {
            console.log("focusMe directiveinit " + scope.focusValue);
            if( scope.focusValue ) {
                $timeout(function() {
                    console.log(" adding focus to element")

                    element[0].focus();
                });
            }
        }
    };
});

```

well this worked for me, the field is focused and also the keyboard is opening

---

<div class="post-metadata">

**Author:** ![maggie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/maggie/32/7923_2.png) [@maggie](https://forum.ionicframework.com/u/maggie)\
**Post date:** [January 13, 2015, 5:24am UTC](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851/21 "2015-01-13T05:24:03Z")

</div>

Oh it works ~~~thanks~~

[Next page](https://forum.ionicframework.com/t/auto-focus-textbox-while-template-loads/6851.md?page=2)
