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
Asked
Active
Viewed 628 times
0
-
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 Answers
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