Yes, there is a way to do this, especially in React. How you should do this is to follow.
Step 1: - use ref in the textarea ui element. as
`<textarea className='html-editor' ref='myTextarea' value = {this.state.textareaVal} onChange={(event)=>{ this.setState({ textareaVal:event.target.value; }); }} > </textarea>`
Step 2: Now you can access the DOM element using the ref reaction.
let textVal = this.refs.myTextarea;
Step 3: - use selectStart and selectionEnd: - using selectionStart and
selectionEnd you can find your start and end pointer
selected text. This can be done as shown below:
let cursorStart = textVal.selectionStart; let cursorEnd = textVal.selectionEnd;
You now have the start and end index of the selected text.
Step 4: - use the javascript substring function to get the selected text.
this.state.textareaVal.substring(cursorStart,cursorEnd)
source share