Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Anton on May 20, 2009 08:03
open dhtmlx forum
How can I set caret to some position in editor?

Problem appears in Firefox. In IE i do
range = element.ownerDocument.body.createTextRange();
range.moveToElementText(element);
                    if (range.findText(element.innerText))
                    {
                        move("word", 0);
                        range.select();
                    }
and it works fine. But firefox doesn't contain createTextRange() method. Can you tell me how can fix it?
Answer posted by Support on May 20, 2009 10:01
In FireFox , Range and Selection objects can be used for the same purpose ( first to define some text range, second to select it ) 
https://developer.mozilla.org/En/DOM/Range
https://developer.mozilla.org/en/DOM/Selection
Answer posted by Anton on May 21, 2009 03:24
Here solution:
var iframes = document.getElementsByTagName("IFRAME");
var currentFrame = iframes[iframes.length - 1]; //get current frame
var sel = currentFrame.contentWindow.getSelection();
sel.collapse(element, 0); // set caret to begin; sel.collapse(element, 1); - sets caret to end