1

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>'));
Iter Ator
  • 8,226
  • 20
  • 73
  • 164

1 Answers1

1

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/.

Community
  • 1
  • 1
SnakeDrak
  • 3,406
  • 4
  • 28
  • 41