Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Content editable remove unnecessary tags with content and all html tags on paste
$('[contenteditable]').on('paste',function(e) {
e.preventDefault();
var text = (e.originalEvent || e).clipboardData.getData('text/html');
var $result = $('<div></div>').append($(text));
// remove unnecesary tags (if paste from word)
$result.children('style').remove();
$result.children('meta').remove();
$result.children('link').remove();
var html = $result.html()
.replace(/<\/?[^`]+?\/?>/gmi, '\n') //replace all tags
.replace(/\n[\s]*\n/gmi, '\n'); //replace many empty lines to one
html = $.trim(html);
var lines = html.split('\n');
for(var l in lines){
lines[l] = '<div>' + $.trim(lines[l]) + '</div>';
}
html = lines.join('\n');
$(this).html(html);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment