I have created a custom @mention system for my app, my only problem is ill start typing @... and then Ill select the user I want to mention and what happens is that the name of the selected user is just added onto the end of the textarea so this works If I only typed in @ but if I typed in @ja and then selected jason smith it would now say @jaJason Smith How Can I remove any text typed after the @ and then add on my string. FYI this is done in a textarea
so my current function looks like this..
addMention(user: any): void {
const textarea = document.querySelector('textarea');
const mentionSelect = <HTMLElement>document.querySelector('.mention-container');
const value = textarea.value; // Store the value
textarea.value = `${textarea.value}${user.value}`; // Current Value
mentionSelect.style.display = 'none'; // Hide the mention list
this.message = textarea.value; // Make sure the new textarea value is stored in message
textarea.focus(); // refocus
textarea.value = ''; // clear
textarea.value = value; // add the value back in
}
so whats happening is Im just updating the value of the textarea by adding on the username
EDIT
It has been brought to my attention this is not the best way to do it since if a user moves the cursor to the middle and types @ the name will be added to the end of the string and not the @ so my question is how can I replace the string after the @ but be able to do it anywhere in the textarea??
any help would be appreciated!
@jain the first place? You're looking at a couple issues. One, you don't remove the characters you used to grab the name before appending and two, you only append to the end of the string. What if they went back and added a mention in the middle?@before this position.textarea.selectionStartjsfiddle.net/7n41o5kq/3