Skip to content

Instantly share code, notes, and snippets.

@jacoor
Created August 14, 2014 18:25
Show Gist options
  • Save jacoor/bce42a3cbd968dca5a03 to your computer and use it in GitHub Desktop.
Save jacoor/bce42a3cbd968dca5a03 to your computer and use it in GitHub Desktop.
clearing clipboard contents from everything except <a href="*">*</a> on paste
elm.bind('paste', function(e) {
//console.log(e);
e.preventDefault();
var text = (e.originalEvent || e).clipboardData.getData("text/html");
if (!text){
console.log('no html');
text = (e.originalEvent || e).clipboardData.getData("text/plain");
}
var temp = $('<div />');
$(temp).html(text);
$(temp).find('*').each(function(index, element){
var text = element.innerText;
var newElement;
if (element.nodeName.toLowerCase() === 'a') {
newElement = document.createElement('a');
newElement.href = element.href;
newElement.innerText = text;
} else {
newElement = document.createTextNode(text);
}
$(element).replaceWith(newElement);
});
document.execCommand("insertHTML", false, temp.html());
$scope.pasted = true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment