New Version : http://jsfiddle.net/Kxmaf/218/
You were right about setTimeout()
, didn't know about the paste event latency.
Now, I saw on FF, that document.execCommand()
is kinda buggy, so I simplified it a lot (no more focus, execCommand):
$editor.on('paste, keydown', function() {
var $self = $(this);
setTimeout(function(){
var $content = $self.html();
$clipboard.val($content);
},100);
});
How about this : http://jsfiddle.net/Kxmaf/215/
$editor.on('paste, keydown', function() {
var $self = $(this),
$content = $self.html();
$clipboard.val('').focus();
document.execCommand('insertHTML', false, $content);
$self.html($content);
placeCaretAtEnd($self);
});
No more setTimeout()
.
I added keydown too as it sounded better to me ;) You can delete it if you will.
This does :
- takes the content of #content
- delete textarea content and focus on it.
- insert the content from #content to the textarea
- replace #content's content in case it cames deleted or replaced (it was part of my test, I don't know if
$self.html($content);
is relevant now.
- focus with caret at the end of #content.
placeCaretAtEnd()
comes from https://stackoverflow.com/a/4238971/460368 which I modified a little.