I'm trying to set focus on a textbox through an onclick trigger and it doesn't seem to work in my ionic angular implementation. It works if I take it out of angluar and ionic
Here's the template
<ion-view title="Create">
<ion-content class="has-header">
<div>
<input type="text" placeholder="focus here" id="test" />
<button ng-click="test()">TEST</button>
</div>
</ion-content>
</ion-view>
And the js
$scope.test = function(){
document.getElementById('test').focus();
};
I found out the answer from the forum: http://forum.ionicframework.com/t/focus-method-doesnt-work-on-input-textarea-etc/5606/14
Turns out you need to add the Ionic Keyboard plugin and add these preferences in the config.xml:
<preference name="KeyboardDisplayRequiresUserAction" value="false" />
<feature name="Keyboard">
<param name="ios-package" value="IonicKeyboard" onload="true" />
</feature>
After that, the focus() function will work.
Easiest and quickest way for me to accomplish this was as follows:
$scope.test = function(){
$timeout(function () {
document.getElementById('test').select();
}, 0);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With