3

I'm facing a annoying problem. I'm working on a little RTE, and I'm now trying to move the caret at the end of an element.

Right now, here is the code I have (from this post How to move cursor to end of contenteditable entity) :

I have some custom functions to work on the range and the window. I'm nearly sure they are correct.

function(contentEditableElement){
    var contentEditableElement = this.getFocusedElement().parentNode;
    if(this.contentDoc.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
    range = this.contentDoc.createRange();//Create a range (a range is a like the selection but invisible)
    range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
    range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
    selection = this.win.getSelection();//get the selection object (allows you to change selection)
    selection.removeAllRanges();//remove any selections already made
    selection.addRange(range);//make the range you have just created the visible selection
 }
else if(this.doc.selection)//IE 8 and lower
{ 
    range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
    range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
    range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
    range.select();//Select the range (make it the visible selection
}
}

It works very well under FF and Opera...but in Chromium, the caret simply does not move.

Can you help me to find the right way ? Thanks

Community
  • 1
  • 1
nioKi
  • 1,259
  • 9
  • 17
  • 1
    It works for me (Chrome 19). See this JSFiddle: http://jsfiddle.net/pTGLy/ I know you have an iframe but it should behave the same. – J. K. Jun 12 '12 at 17:46
  • It's working very well on the JSFiddle, but not with the IFrame. I read somewhere that there were a bug with WebKit, and people gave some workaround. In my Iframe, if I add some characters at the end of the element (

    likeTHIS

    ) then I'm able to set the caret at the end of

    . If it's empty, then I cannot. It seems I'm gonna have to do some kind of buffering.

    – nioKi Jun 12 '12 at 22:50
  • Yep, if it's empty, it does not work. That is a bug. I even thought it was not WebKit-specific. – J. K. Jun 12 '12 at 23:09

0 Answers0