get text under current cursor positon inside a textarea

is there a way to get a string under current cursor position inside a textarea
for example – click on lorem – console should be lorem
essentially, something like this:

  let a = previous space or line break from cursor position
  let b = next space or line break from cursor position
  console.log(text from a to b);
$('#ed').on('click', function(){
  //let a = previous space or line break from cursor position
  //let b = next space or line break from cursor position
  //console.log(text from a to b);
});
#ed{
display:block;
width:100%;
padding:9px;
height:50vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id='ed'>
lorem ipsum
https://google.com
</textarea>

Leave a Comment