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>