Problem with createRange in Firefox.

User Help for Mozilla Firefox
Post Reply
gli
Guest

Problem with createRange in Firefox.

Post by gli »

Hi,

I am coding a small RTE and I want to hilite some text(not the hilitecolor, which gives background to the text, instead I want a color to the text instead).
So I am writing a function, that takes the selection, take the text from the selection and insert it in between the <span>range.text</span>

It works fine with IE.

But in firefox I am unable to get the text of the selection range. Can anyone help me please.

here is the snippet

cDoc = document.getElementById(textEditorName).contentWindow.document;
if(document.selection){

range=cDoc.selection.createRange();
range.pasteHTML("<span>"+range.text+"</span>");

} else {

range = cDoc.createRange();
alert(range.text);
}

I have seen the gecko reference for createRange
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);

Notes
Once a Range is created, you need to set its boundary points before you can make use of most of its methods.

how to get the startNode, since it is a plain text element in the iframe.

Thanks in advance,
gli
gli
Guest

I got in completely wrong.

Post by gli »

:oops:

I got the solution and the solution makes me look stupid. It was so simple.

so here is the snippet

if(document.selection){
cDoc = document.getElementById(textEditorName).contentWindow.document;
range=cDoc.selection.createRange();
range.pasteHTML("<span>"+range.text+"</span>");
cDoc.selection.empty();
} else if (window.getSelection) {

cWin = document.getElementById(textEditorName).contentWindow;
userSelection = cWin.getSelection();
hiliteText = "<span>"+userSelection+"</span>";
textEditor.contentWindow.document.execCommand('insertHTML',false,hiliteText);
} :x ](*,)
Post Reply