What's the best way to set cursor/caret position?

Posted by Daniel Bachhuber on Stack Overflow See other posts from Stack Overflow or by Daniel Bachhuber
Published on 2009-08-10T06:04:30Z Indexed on 2010/04/25 18:53 UTC
Read the original article Hit count: 213

If I'm inserting content into a textarea that TinyMCE has co-opted, what's the best way to set the position of the cursor/caret?

I'm using tinyMCE.execCommand("mceInsertRawHTML", false, content); to insert the content, and I'd like set the cursor position to the end of the content.

Both document.selection and myField.selectionStart won't work for this, and I feel as though this is going to be supported by TinyMCE (through something I can't find on their forum) or it's going to be a really ugly hack.

Later: It gets better; I just figured out that, when you load TinyMCE in WordPress, it loads the entire editor in an embedded iframe.

Later (2): I can use document.getElementById('content_ifr').contentDocument.getSelection(); to get the selection as a string, but not a Selection Object that I can use getRangeAt(0) on. Making progress little by little.

© Stack Overflow or respective owner

Related posts about tinymce

Related posts about Wordpress