Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Prompt Alert focus on text input

I am creating an ionic 2 application for android and ios. I have an alert controller that prompts the user for a text input, however when the alert pops up I would like the keyboard to focus on the input, and for the placeholder text to be highlighted. Currently you must tap on the text input for the keyboard to pop up. Is this possible to implement?

here is my alert controller code:

let prompt = this.alertCtrl.create({
    title: this.name,
    message: this.nameMessage,
    inputs: [
      {
        name: 'name',
        placeholder: this.name
      },
    ],
    buttons: [
      {
        text: this.save,
        handler: data => {
          resolve(data.name);
        }
      }
    ],
    enableBackdropDismiss: false
  });
  prompt.present();
like image 790
James Griess Avatar asked Mar 02 '26 16:03

James Griess


1 Answers

This is my solution.

First

inputs: [
    {
      id: "autofocu",
      ......
    }
  ],

And then

alert.present()
.then(() => {
  document.getElementById('autofocu').focus();
})
.catch()
like image 85
user8539302 Avatar answered Mar 06 '26 04:03

user8539302



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!