Dropdown value patch based on cursor position in textarea
you need take account the "selectionStart" and selectionEnd of the textArea. For this, create a template reference variable and pass to the function
<!--see the #text-->
<textarea #text [(ngModel)]='textValue' rows="10" cols="70">
</textarea>
...
<!--pass the "variable" to the function changeValue-->
<mat-select (change)="changeValue($event,text)">
...
</mat-select>
Then your function changeValue
changeValue(ev,textArea:any) {
const posInitial=textArea.selectionStart;
const posFinal=textArea.selectionEnd;
this.textValue = this.textValue.substr(0,posInitial)+
ev.value+
this.textValue.substr(posFinal);
}
NOTE: If you don't want pass the "text" to the function you can also use ViewChild
@ViewChild("text") textArea:ElementRef
And to get the positions using
const posInitial=this.textArea.nativeElement.selectionStart;
const posFinal=this.textArea.nativeElement.selectionEnd;
See that in this case you use this.textArea.ElementRef
stackblitz
Set text-cursor position in a textarea
After refocusing the textarea with txtarea.focus()
, add this line:
txtarea.selectionEnd= end + 7;
That will set the cursor seven positions ahead of where it was previously, which will take [b][/b]
into account.
Example
document.getElementById('bold').addEventListener('click', boldText);
function boldText() { var txtarea = document.getElementById("editor_area"); var start = txtarea.selectionStart; var end = txtarea.selectionEnd; var sel = txtarea.value.substring(start, end); var finText = txtarea.value.substring(0, start) + '[b]' + sel + '[/b]' + txtarea.value.substring(end); txtarea.value = finText; txtarea.focus(); txtarea.selectionEnd= end + 7;}
#editor_area { width: 100%; height: 10em;}
<button id="bold">B</button><textarea id="editor_area"></textarea>
get text under current cursor positon inside a textarea
You are wellcome,
var borderChars = [' ', '\n', '\r', '\t']
$(function() {
$('textarea').on('click', function() {
var text = $(this).html();
var start = $(this)[0].selectionStart;
var end = $(this)[0].selectionEnd;
while (start > 0) {
if (borderChars.indexOf(text[start]) == -1) {
--start;
} else {
break;
}
}
++start;
while (end < text.length) {
if (borderChars.indexOf(text[end]) == -1) {
++end;
} else {
break;
}
}
var currentWord = text.substr(start, end - start);
console.log(currentWord);
});
});
Related Topics
How to Inspect CSS Pseudo Classes with Firebug
Background Image Stretch Y-Axis Only, Keep Repeat-X
Ionic - Ion-Item Text Is Not Vertically Centered When Ion-Icon Is Bigger
How to Add a Scroll Bar to a Component in React
How to Left Align Bootstrap 3 Dropdown Menu
Getting Image to Stretch a Div
CSS How to Position an Element in a Middle (Half Height/Vertical 50%) of Another Element
Glyphicon Works Locally But Not on Heroku
What's The Point of Gutters in CSS Grid Frameworks
Div with Margin-Left and Width:100% Overflowing on The Right Side
Material UI V1 - Set Table Column Widths
One CSS Declaration for All CSS Font Properties
How to Use SASS to Properly Avoid Embedding Twitter Bootstrap Class Names on HTML
How to Override Scoped Styles in Vue Components
How to Use Font Awesome in Own CSS
Vertical Alignment Based on X-Height