in the example below pls place the cursor inside story
, for example after the letter b
.
When clicking on the button I need to expand the selection left AND right untill a white space - or start/end of line.
so https://tumblr.com
should be selected and written in console.
problem with my trying - only tumblr
is selected
$(btngo).on('click', function(){
var sel = window.getSelection();
sel.modify("move", "backward", "word");
sel.modify("extend", "right", "word");
console.log(sel.toString()); // tumblr - I need - https://tumblr.com
});
.story{outline:none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='story' id='story' contenteditable>
lorem - https://tumblr.com
</div>
<br>
<button id='btngo'>GO</div>
CodePudding user response:
Place cursor within text and click "GO". The entire text block is split on spaces. This array is looped over searching for the cursor position. Once it is found the word in the array is selected and returned.
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='story' id='story' contenteditable>
lorem - https://tumblr.com
</div>
<br>
<button id='btngo'>GO</button>
Javascript
// document.getElementById("btngo").addEventListener('click', function () { // vanilla
$(btngo).on('click', function(){
var sel = window.getSelection();
// get beginning and end of selection
let ao = sel.anchorOffset;
let fo = sel.focusOffset;
// make sure there is a cursor position but not a selected range
if ( 1 < fo && ao === fo ) {
// get node containing cursor
let fn = sel.focusNode;
// trim whitespace at beginning and end of block of text,
// then split on spaces into an array
let fn_arr = fn.textContent.trim().split(' ');
// loop array and search for cursor position
// when found use position to determine word in array containing cursor
let [strt,end,wrd] = fn_arr.reduce(function (acc, txt) {
return (fo > acc[1]) // have we reached the cursor position?
? [acc[1] 1, acc[1] txt.length 1, txt] // if not keep track of location
: acc; // if so, keep returning location information
}, [0,0,""]);
console.log(wrd); // display word containing cursor
// select word containing cursor
let range = new Range();
range.setStart(fn, strt);
range.setEnd(fn, end);
sel.addRange(range);
}
else { console.log('no cursor position') }
});
CodePudding user response:
You can use a range to position the cursor where you need it and select from there, in this case the selection will be from the 9th position up to the end of line.
$(btngo).on('click', function(){
var range = document.createRange()
var sel = window.getSelection()
//place the cursor to 9th position
range.setStart($("#story")[0].childNodes[0], 9)
range.collapse(true)
sel.removeAllRanges()
sel.addRange(range)
sel.modify("extend", "right", "lineboundary");
console.log(sel.toString());
});
.story{outline:none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='story' id='story' contenteditable>
lorem - https://tumblr.com
</div>
<br>
<button id='btngo'>GO</div>