0

My startposition gets changed when I select some text with "shift + arrow_left" let me explain in example:

Example: - contenteditable div - inserted charcode 0182 (¶) by myself = the variable: hardReturn

text in div: test¶ abc

code on key up + arrow_left:

    var myRange = document.selection.createRange(); 
myRange.moveStart('character', -1); 
var charVoor = myRange.text.substring(0, 1);

if(charVoor == hardReturn){
    myRange.moveEnd('character', -1);
    myRange.select();   
}else{
} 

So my problem: my cursor is between "b" and "c", i start selecting... I got "ab" under selection, then I do another selection to the left, my selection will jump automaticly before the "¶", but after that my startposition of my selection is before "¶" and not between "b" & "c" where I started.

Ziggiej
  • 119
  • 1
  • 1
  • 7

1 Answers1

0

You could use a clone of the TextRange object to check hardReturn:

var myRange = document.selection.createRange();
var chkRange = myRange.duplicate();
chkRange.moveStart('character', -1); 
var charVoor = chkRange.text.substring(0, 1);

if(charVoor == hardReturn){
    chkRange.moveEnd('character', -1);
    ....  
}

I'm not sure what is the final text you want in the selection. If needed, you can move the original myRange in relation to chkRange with setEndPoint() method, or use collapse() to collapse the range between a and b.

You can find setEndPoint() and other methods and properties in TextRange from MSDN: Traversal and Range

Teemu
  • 22,918
  • 7
  • 53
  • 106