Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How do I surround selected text with arbitrary HTML in FCKEditor?

I'm using FCKEditor in a web application.  I'm creating custom plugins which allow users to select from a list of drop downs and apply standard formatting to the selected text.
I can easily apply divs and such using ApplyStyle, what I haven't been able to figure out is how I surround the selected text with something like the following.
Selected Text would be here
I can do the fieldset and the div by using ApplyStyle multiple times, what I haven't figured out is how to add the Legend.
1 Solution
dguillenAuthor Commented:
I found an answer in an obscure post.

// get HTML from selection
function getSelectionHTML(selection) {
    var range = (document.all ? selection.createRange() : selection.getRangeAt(selection.rangeCount - 1).cloneRange());

    if (document.all) {
        return range.htmlText;
    else {
        var clonedSelection = range.cloneContents();
        var div = document.createElement('div');
        return div.innerHTML;

its used by doing the following

            var selection = "";

            if (FCK.EditorDocument.selection != null) {
                selection = getSelectionHTML(FCK.EditorDocument.selection);
            } else {
                selection = getSelectionHTMLFCK(EditorWindow.getSelection()); // after this, won't be a string
                selection = "" + selection; // now a string again

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now