0

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>
SeaBass
  • 1,584
  • 4
  • 20
  • 46
  • can you show a piece of your code, you might need to convert \n to
    – bdalina Jul 09 '18 at 02:32
  • 2
    Please permit us to recreate your issue, by offering some reasonably accurate input data, your code, and your expected output. – mickmackusa Jul 09 '18 at 02:43
  • Possible duplicate of [InnerHTML linebreaks with a string that is very long with line breaks, coming from a MYSQL query](https://stackoverflow.com/questions/43950487/innerhtml-linebreaks-with-a-string-that-is-very-long-with-line-breaks-coming-fr) – caiovisk Jul 09 '18 at 02:43
  • @bdalina code added – SeaBass Jul 09 '18 at 05:32
  • @mickmackusa updated – SeaBass Jul 09 '18 at 05:32
  • have you tried nl2br()? a php function – bdalina Jul 09 '18 at 05:43
  • https://stackoverflow.com/questions/5048849/preserve-line-breaks-from-textarea-when-writing-to-mysql – bdalina Jul 09 '18 at 05:43
  • @bdalina Thanks! I tried to replace my span with pre in my snippet, but same result. Try to remove all the text in my contenteditable and type something on different lines. It will all be placed on one line with text() and it will contain html code with html(). I had nl2br before but since I can't figure out how to save the data to the database with pure line-breaks nl2br() isn't helping unfortunately. Is there something I need to do on span keypress to tackle what the enter key should do? E.g. add \n not
    or
    etc.?
    – SeaBass Jul 09 '18 at 06:02
  • when you are retrieving the data just simply add htmlspecialchars_decode() – bdalina Jul 09 '18 at 06:09
  • if you are bothered with XSS attack you may want to use strip_tags($input_post) then after removing the tags use the nl2br() function and when you retrieve the text convert it using htmlspecialchars_decode(); so the
    will be converted to newline
    – bdalina Jul 09 '18 at 06:11
  • @bdalina Sorry I'm missing something here. My problem is before, not after the database save. I don't want to alter the data going into the database. Right now it either saves without linebreaks or with html code, escaped or unescaped not just text with /n. – SeaBass Jul 09 '18 at 06:11
  • I think I just switch and do input/textarea without any styling instead. – SeaBass Jul 09 '18 at 06:48

0 Answers0