Jauzsika
Jauzsika

Reputation: 3251

Move cursor to the beginning of a designmode iframe

Is it possible to move the caret to the very beginning (right before first element) of a designmode IFRAME?

Upvotes: 1

Views: 853

Answers (1)

Tim Down
Tim Down

Reputation: 324627

Here's a function to do that. Pass in a reference to the <iframe> element.

Live demo: http://jsfiddle.net/aLP26/

Code:

function moveCursorToStart(iframeEl) {
    var win = iframeEl.contentWindow || iframeEl.contentDocument.defaultView;
    var doc = win.document;
    if (win.getSelection && doc.createRange) {
        var sel = win.getSelection();
        var range = doc.createRange();
        range.selectNodeContents(doc.body);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (doc.selection && doc.body.createTextRange) {
        var textRange = doc.body.createTextRange();
        textRange.collapse(true);
        textRange.select();
    }
}

Upvotes: 4

Related Questions