I'm currently developing a simple calculator app and I'm facing an issue.
My goal is to add text at the caret position of an input text field when a button is clicked. However, the code snippet provided below is not functioning as expected:
At the line
strPos = inputText.selectionStart;
, the code seems to stop working without any errors being thrown. Is there anyone who can suggest a solution for this?
Edit
Here is a snippet from my page1.html file:
<ion-input #myInput type="text" [value]="formula" (focus)="onFocus($event)"></ion-input>
<button class="button" id="number" (click)="numberTapped(myInput, 1)">1</button>
And here is a portion of my page1.ts file:
onFocus($event){
this.currentInput = document.activeElement;
}
numberTapped($event, number) {
this.insertAtCaret($event, String(number))
}
insertAtCaret(inputId,text) {
var inputText:any = myInput; //document.getElementById(inputId);
var strPos = 0;
strPos = inputText.selectionStart;
var front = (inputText.value).substring(0,strPos);
var back = (inputText.value).substring(strPos,inputText.value.length);
inputText.value=front+text+back;
strPos = strPos + text.length;
inputText.selectionStart = strPos;
inputText.selectionEnd = strPos;
inputText.focus();
}