0

I have a bootstrap panel that contains text that was previously inserted by the user. The user text may contain new lines.

What's the best way do display those new lines? I've found this answer and it works if I put a <p> element inside the div. Another answer seems impossible since it would require text replacing, that may be problematic.

Is this the best approach? Will it work on major browsers?

Community
  • 1
  • 1
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82

3 Answers3

0

I think you can try use <pre> HTML-tag. In this case new-lines will be displayed as is

ZFTurbo
  • 3,652
  • 3
  • 22
  • 27
0

jQuery solution that might work

$(document).ready(function() {
  $.each($('.panel-body'), function(k, panel_body) {
    $(panel_body).html($(panel_body).text().replace(new RegExp('\r?\n','g'), '<br />'));
  });
});
  • I was trying to avoid text replacement, cause I'm pretty sure that some edge case will be broken with such approach – pedrorijo91 Oct 21 '15 at 22:36
0

my solution was:

<p style="white-space: pre">user-text</p>

it seems to be working correctly

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82