0

In the example below place the cursor anywhere in the middle of two lines, eg. from 'ipsum' to the following 'lorem'. The following words will be selected: ipsum dolor sit lorem

On clicking the button I need to get the full lines in the selection. In the above example I should retrieve the following words: lorem ipsum dolor sit lorem ipsum

Does anyone know how I can do this?

$('button').on('click', function() {
  var sel = window.getSelection();
  sel.modify('move', 'backward', 'paragraphboundary');
  sel.modify('extend', 'right', 'paragraphboundary');
});
div {
  white-space: pre-wrap;
  outline: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div contenteditable>lorem ipsum
dolor sit
lorem ipsum

lorem ipsum
dolor sit
lorem ipsum
</div>
<br>
<button>CLICK</button>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
provance
  • 877
  • 6
  • 10
  • It was answered already,here is the post https://stackoverflow.com/questions/1173194/select-all-div-text-with-single-mouse-click – Nexo Aug 31 '22 at 18:32
  • @Nikkkshit - seems it is not answered on your link. At least - not in the accepted answer – provance Aug 31 '22 at 18:36
  • Yes it is, the method in the accepted answer works fine: https://jsfiddle.net/wrgxyhu2/ – Rory McCrossan Aug 31 '22 at 18:52
  • @RoryMcCrossan - no, it is not. Please read my question carefully - `block between...` – provance Aug 31 '22 at 19:11
  • Did you check the fiddle example? The position of the cursor has absolutely *no bearing* on the result - it will always select all text in the element. – Rory McCrossan Aug 31 '22 at 19:18
  • @RoryMcCrossan - of course I tried. I don't need `entire text` - I need entire `block of text` between `\n\n` and `\n\n` or between start and next `\n\n` - just as I wrote in post – provance Aug 31 '22 at 19:22
  • In that case your terminology needs to be improved. I will edit your question. – Rory McCrossan Aug 31 '22 at 19:23
  • @RoryMcCrossan - I'm sure - my terminology - between start and `\n\n` - or between two `\n\n` - is more precize. But never mind, thanks – provance Aug 31 '22 at 19:34

0 Answers0