Forum Moderators: open
// using jQuery 3.3.1
var source = $('#original-contenteditable');
var clipboard = $('#clipboard-contenteditable');
var a;
source.on('beforepaste', function(e) {
if (window.clipboardData) {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
clipboard.focus();
// no clue what these next 7 lines do... I saw them elsewhere, but it works the
// same with or without them
var range = document.createRange();
range.selectNodeContents(clipboard.get(0));
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
var getRange = sel.getRangeAt(0);
getRange.collapse(false);
////////
// not sure why, but if it's not delayed a little then it doesn't work
setTimeout(function() {
a = clipboard.html();
// manipulate data
a = editClip(a);
// this is where the problem is, it focuses back to the top of the element
// instead of where the caret originally was
source.focus();
document.selection.createRange().pasteHTML(a);
return;
}, 30);
}
})
// This works fine with Chrome, FF, and Android browser
.on('paste', function(e) {
if (e.originalEvent.clipboardData) {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
a = (e.originalEvent || e).clipboardData.getData('text/html');
// manipulate data
a = editClip(a);
document.execCommand('insertText', false, a);
return;
}
});
window.clipboardData.getData('Text');