# How to keep focus in ion-input with clearInput

**URL:** <https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945>\
**Category:** ionic-v3\
**Created:** [August 5, 2017, 8:31am UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945 "2017-08-05T08:31:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xgi](https://avatars.discourse-cdn.com/v4/letter/x/7feea3/32.png) [@xgi](https://forum.ionicframework.com/u/xgi)\
**Post date:** [August 5, 2017, 8:31am UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/1 "2017-08-05T08:31:09Z")

</div>

When adding clearInput=“true” to ion-input, a clear icon appears and clicking to it clears the input  
… but input looses the focus (and, on android device, soft keyboard gets also hidden).

How can I keep the focus in the input (and the keyboard displayed) so that when clicking on clear icon, user can start typing the new input value straight, without the need to (re)select it again?

Thanks in adavance…

ionic info:

cli packages: (/Users/xaviergillmann/Workspace/repo/ionic/sobillit/node\_modules)

```
@ionic/cli-plugin-cordova : 1.6.2
@ionic/cli-plugin-ionic-angular : 1.4.1
@ionic/cli-utils : 1.7.0
ionic (Ionic CLI) : 3.7.0

```

global packages:

```
Cordova CLI : 7.0.1 

```

local packages:

```
@ionic/app-scripts : 1.3.7
Cordova Platforms : android 6.2.3
Ionic Framework : ionic-angular 3.6.0

```

System:

```
Node : v7.9.0
OS : macOS Sierra
npm : 5.0.1
```

---

<div class="post-metadata">

**Author:** ![enekodr](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@enekodr](https://forum.ionicframework.com/u/enekodr)\
**Post date:** [December 19, 2017, 1:48pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/2 "2017-12-19T13:48:34Z")

</div>

Actually I was not able to find the solution for 1 month, and that makes me think that it is not possible yet.

The same thing happens when trying to show a password by clicking an ‘eye’ button inside the input field.

The app simply knows that user touched something out the input field, and it makes it to loose the focus.

---

<div class="post-metadata">

**Author:** ![SigmundFroyd](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sigmundfroyd/32/111604_2.png) [@SigmundFroyd](https://forum.ionicframework.com/u/SigmundFroyd)\
**Post date:** [December 19, 2017, 3:27pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/3 "2017-12-19T15:27:34Z")

</div>

I saw something on Josh Morony’s blog the other day where he mentioned that if you use `(touchStart)` instead of `(click)` your inputs won’t lose focus.

---

<div class="post-metadata">

**Author:** ![dorkor231](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dorkor231](https://forum.ionicframework.com/u/dorkor231)\
**Post date:** [July 9, 2018, 5:31pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/4 "2018-07-09T17:31:37Z")

</div>

i have the same problem  
//here the item  
  
Puntaje obtenido  
\<ion-input type=“number” #puntajeObtenido\>  
  
//when i try to focus  
const prompt = this.alertCtrl.create({  
title: ‘Nota’,  
message: "Su nota es: " + total,

```
  buttons: [
    {
      text: 'OK',
      handler: data => {

```

// here i try to focus when the user click in ok  
this.puntajeObtenido.setFocus();  
}  
}  
]  
});  
prompt.present();  
// i focus the input when the alert end  
this.puntajeObtenido.setFocus();

same problem, the input focus and then 1 sec later the keyboard hide and lose the focus  
sorry about my english

---

<div class="post-metadata">

**Author:** ![jjacobs](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jjacobs](https://forum.ionicframework.com/u/jjacobs)\
**Post date:** [September 12, 2018, 8:51pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/5 "2018-09-12T20:51:03Z")

</div>

I have the same problem. When I hit clearInput it’s because I want to enter something else in the inputbox but it loses focus after the input box is cleared and the user has to tap it again.

Is there a way to fix this?

---

<div class="post-metadata">

**Author:** ![jegodard](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@jegodard](https://forum.ionicframework.com/u/jegodard)\
**Post date:** [November 11, 2018, 6:00pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/6 "2018-11-11T18:00:05Z")

</div>

Did you manage to find a workaround to make it work?

---

<div class="post-metadata">

**Author:** ![Unkn0wn0x](https://avatars.discourse-cdn.com/v4/letter/u/46a35a/32.png) [@Unkn0wn0x](https://forum.ionicframework.com/u/Unkn0wn0x)\
**Post date:** [May 3, 2019, 10:54pm UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/7 "2019-05-03T22:54:15Z")

</div>

I’ve found a solid solution, which works across all platforms, including Android and iOS. Here you go:

**MARKUP**

```auto
<ion-input [(ngModel)]="myInputNgModel" clearInput></ion-input>

```

**TYPESCRIPT**

First we need to create a little helper function to prevent default events and event propagation:

```auto
    /**
	 * @private
	 * @function stopInputBubble
	 * @description Helper function to stop default events and event propagation
	 * @param {any} evt The event, which was initially triggered and should be prevented
	 * @return {void} 
	 */
	private stopInputBubble(evt: any) {

		evt.preventDefault();
		evt.stopPropagation();
		return;
	}

```

Now we need to create the function itself, which prevents closing the Keyboard on hitting the clear button within the `<ion-input>`:

```auto
    /**
	 * @protected
	 * @function clearInput
	 * @description Helper function to prevent closing the keyboard on hitting the clear button
	 * @return {void}
	 */
	protected clearInput(): void {

		// Create a variable, storing the native button element, alternatively use a `@ViewChild`
		const elem: HTMLElement = document.querySelector('button.text-input-clear-icon.button-clear');

		// Programatically add multiple event listeners
		['click', 'mousedown', 'touchdown', 'touchmove', 'touchstart'].forEach((evt: any) => {
			elem.addEventListener(evt, this.helperCtrl.stopInputBubble, false);
			return;
		});

		// Add event listener for `touchend`, triggered by a mobile device
		elem.addEventListener('touchend', (evt: any) => {
			this.stopInputBubble(evt);
			this.myInputNgModel = '';
			return;
		});

		// Add event listener for `mouseup`, triggered by a browser
		elem.addEventListener('mouseup', (evt: any) => {
			this.stopInputBubble(evt);
			this.myInputNgModel = '';
			return;
		});
		return;
	}

```

Don’t forget to call `this.clearInput()` whilst initializing your module, for example in `IonViewDidLoad` or something like this.

I hope it helps someone, whether it’s a little bit hacky or not 🙂.

Cheers  
Unkn0wn0x

---

<div class="post-metadata">

**Author:** ![esneideramy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/esneideramy/32/119524_2.png) [@esneideramy](https://forum.ionicframework.com/u/esneideramy)\
**Post date:** [December 5, 2019, 2:48am UTC](https://forum.ionicframework.com/t/how-to-keep-focus-in-ion-input-with-clearinput/100945/8 "2019-12-05T02:48:46Z")

</div>

> [@Unkn0wn0x](#):
>
> /\*\* \* @private \* @function stopInputBubble \* @description Helper function to stop default events and event propagation \* @param {any} evt The event, which was initially triggered and should be prevented \* @return {void} \*/ private stopInputBubble(evt: any) { evt.preventDefault(); evt.stopPropagation(); return; }

The only real solution! thanks a lot!! 😃
