2

I want to add selected value from dropdown to be displayed in text box at position cursor is present. I'm using .

enter code selectChangeHandler(event: any) {
this.selectedID = event.target.value;
// console.log("this.selectedID", this.selectedID);
this.arrayfordynamic += `<div>{{DV.Product.${this.selectedID}}}</div>`
console.log("this.arrayfordynamic", this.arrayfordynamic)
this.productDiscriptionArrayForID = `<div>${this.arrayfordynamic} </div> ${this.product.productDescription}`
// console.log("this.selectedIDwithDiscription-----", this.arrayfordynamic);
this.productForm.controls.des.setValue(this.productDiscriptionArrayForID);
}

I'm setting dropdown value into the angular-editor text. But I wan't to set this value at cursor position when I click on dropdown value. Please let me know how we can do that.

2 Answers 2

2

Let's say this is your input:

<input id="myInput"type="text">

You can get position by this function:

function getCursor() {
  let cursor = document.getElementById('myInput');
  let start = cursor.selectionStart;
  let end = cursor.selectionEnd;
  return [start, end];
}

Now you have to write a function to patch value:

function patchValue(selectedValue) {
  let currentValue = document.getElementById('myInput').value;
  let cursor = getCursor();
  let patchedValue = currentValue.substr(0, cursor[0]) + selectedValue + currentValue.substr(cursor[1]);
  document.getElementById('myInput').value = patchedValue;
}

You can even select a range and replace that range with your selected value with this code.

Here's an example:

function getCursor() {
  let cursor = document.getElementById('myInput');
  let start = cursor.selectionStart;
  let end = cursor.selectionEnd;
  return [start, end];
}

function patchValue() {
  let currentValue = document.getElementById('myInput').value;
  let cursor = getCursor();
  let patchedValue = currentValue.substr(0, cursor[0]) + document.getElementById('myPatch').value+ currentValue.substr(cursor[1]);
  document.getElementById('myInput').value = patchedValue;
}
<label>patch:</label><input id="myPatch"type="text">
<br>
<label>input:</label><input id="myInput"type="text">
<br>
<button onclick="patchValue()">patch</button>

Sign up to request clarification or add additional context in comments.

4 Comments

Property 'selectionStart' does not exist on type 'HTMLElement'. getting this error. Did I need to import something for solving that error
I have to see the code. but if you follow the snippet logic you will not have any issues.
I already shared code above
` getCursor() { let cursor = document.getElementById('f2'); let start = cursor.selectionStart; let end = cursor.selectionEnd; return [start, end]; }`
1

Add "as HTMLInputElement" to it and everything works perfect.

 getCursor() {
    let cursor = document.getElementById('texto-value') as HTMLInputElement;
    let start = cursor.selectionStart;
    let end = cursor.selectionEnd;
    return [start, end];
  }

1 Comment

Welcome to SO. Please note, that this is the English-only version of the site. You'll find the Spanish site at es.stackoverflow.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.