how to insert text and tags at the cursor CKEDITOR

//change all "IDofEditor" to the id of your editor; I haven't figured out how to find it less explicitly

//insert text
CKEDITOR.instances.IDofEditor.insertText('some text here');

//insert a link (or other tags, modify as needed)
//this example uses the selected text for the link text
//if removing getSelection() and just inserting it all, you MUST have some link text or it will NOT insert; also if the user selected text first anyway it will change it to "NaN".
//the \x22 represents a double quote, thus easy to use within an html onclick
//getNative() gets the text, otherwise you get an object
CKEDITOR.instances.IDofEditor.insertHtml('<a href=\x22my_link\x22>' + CKEDITOR.instances.IDofEditor.getSelection().getNative() + '</a>');

//you could also prompt the user for the link text (and title and link or anything else); very fast entry!
CKEDITOR.instances.IDofEditor.insertHtml('<a title=\x22' + prompt('Title:','') + '\x22 href=\x22' + prompt('URL:','http://') + '\x22>' + prompt('Link Text:','') + '</a>');

//put quotes (or any text) around selected text (ckeditor uses the q tag for "Inline quotes" which doesn't work in ie7-)
//use insertHtml() otherwise you get the text "&ldquo;", but for regular text, use insertText()
//getNative() gets the text, otherwise you get an object
CKEDITOR.instances.IDofEditor.insertHtml('&ldquo;' + CKEDITOR.instances.IDofEditor.getSelection().getNative() + '&rdquo;')

//put html tags around something
//haven't tried yet, but I assume you can alter the above to do it

//also getData()  gets everything in the editor

Comments

Popular posts from this blog

Add Image through base64 on PDF in itextsharp

Set span value using jquery and javascript