Skip to content

Instantly share code, notes, and snippets.

@renehuber
Created June 20, 2012 11:38
Show Gist options
  • Save renehuber/2959472 to your computer and use it in GitHub Desktop.
Save renehuber/2959472 to your computer and use it in GitHub Desktop.
Plaintext contentEditable DIVs
/**
* Event handler to prevent richtext and linebreaks in contentEditable DIVs
*/
$('body').on('keydown paste', 'div.editfield', function(e) {
var $field = $(e.currentTarget);
if (e.keyCode===13 && $field.hasClass('multiline')) {
return true;
} else if (e.keyCode===13 || e.type==='paste') {
setTimeout(function() {
$field.html($field.text());
},0);
}
});
@renehuber
Copy link
Author

Html-Elements with contentEditable go weird if the user pastes richt-text content into it or forces a line-break. This hack replaces richtext- right after pasting with simple plaintext, but works only with DIV-elements (in Firefox, as far as i know)

@amalts
Copy link

amalts commented Jan 6, 2015

Thanks a lot for this hack :)

@CavalcanteLeo
Copy link

works like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment