0

I want to make a new WYSIWYG editor, in which I have used an Iframe with designmode ON, I Want to do something like - when user selects a text and click an image button the image should be in background of the text and the image should be toggle

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
James
  • 237
  • 1
  • 3
  • 8
  • 1
    _... in IE it is not working..._: what have you tried then ? We can help you if we have a bit of a coding context. – Didier Ghys Jan 05 '12 at 09:02

1 Answers1

1

The InsertHTML command does not work in IE. However, IE's TextRange object has a convenient pasteHTML() method that you can use instead.

Live demo: http://jsfiddle.net/RmXgy/1/

Code:

function getSelectedText() {
    var selectedText = "", sel;
    if (window.getSelection) {
        selectedText = "" + window.getSelection();
    } else if ( (sel = document.selection) && sel.type == "Text") {
        selectedText = sel.createRange().text;
    }
    return selectedText;
}

var sel, html = '<span style="background-image: url(foo.png)">'
         + getSelectedText() + "</span>";

if ( (sel = document.selection) && sel.type != "Control") {
    sel.createRange().pasteHTML(html);
} else {
    document.execCommand("InsertHTML", false, html);
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536