angular - Select last three digits of input on focus -


is possible automatically select last 3 characters of input field when field receives focus.

for example, input field has number "123456". when field receives focus, want "456" selected can type "789" , input have value "123789".

i'm using ionic 3.

this solution uses elementref comes warning vulnerable xss-attacks. , used last resort.

html:

<ion-item>     <ion-input type="text" #testinput (focus)="onfocus()">     </ion-input> </ion-item> 

ts:

import { component, viewchild, elementref } '@angular/core'; ...   export class testpage{     @viewchild('testinput', { read: elementref }) input:elementref;     private inputref;      constructor(){}      ionviewdidload(){         this.inputref = this.input.nativeelement.queryselector('input');     }      onfocus(){         let length = this.inputref.value.length;          if(length > 3){             this.inputref.setselectionrange(length - 3, length)         }         else{             this.inputref.select();         }     }     ... } 

Comments

Popular posts from this blog

javascript - Replicate keyboard event with html button -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -