Reputation: 9334
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
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