Is there a way I can use .text()
and preserve the linebreaks from a contenteditable field? I tried .html()
, but I get a scrambled result with escaped tags not good for database storage.
Ideas?
Updated with code and explanation:
$(document).on('blur', '[contenteditable]', function(e) {
var self = $(this);
var value = self.html();
// value = htmlToText(value); // This function works, but seemd very hacky. https://github.com/vorushin/jsHtmlToText
alert(value);
/*$.ajax({ type: "POST",
url: "?ajax=save_data",
data: { value: value }
}).done(function(data) {
// Done
});*/
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span contenteditable="true" style="white-space: pre-line;">Editable Text
with line-breaks.
Try typing any html code and blur.
I want to save this data exactly the way I'm typing it, not escaped.
I tried .text() but that doesn't give me the line-breaks</span>
</div>