Iter Ator
Iter Ator

Reputation: 9334

Insert text at the caret position, inside a contenteditable body of a frame

I have an iframe object, and there is a contenteditable body inside. I would like to paste some text, or html element at the caret position. I tried this code, but I've got an error Cannot read property 'createRange' of undefined

$('#edit_frame').selection.createRange().pasteHTML($('<span>text</span>'));

Upvotes: 1

Views: 838

Answers (1)

SnakeDrak
SnakeDrak

Reputation: 3642

Because you need get the iFrame's document.

// HTML selected
document.getElementById('edit_frame').contentWindow.document.selection.createRange().htmlText; 

But it's IE only. Read the @Tim Down answer: https://stackoverflow.com/a/6668159/2389232.

I have adapted the function to get selection HTML for iFrames too. See http://jsfiddle.net/4bp42891/.

Upvotes: 1

Related Questions