0

I have a json file which contains some texts, and some of these texts could have some breaklines

"surchargeNoteText":"Please note \r \n that pre-payments will take up to 2 business \r \n days to be allocated to your reservation",

I tried to use \r and/or \n and the result was this enter image description here

So, the funny thing is that when inspecting the html, the output is right, with 2 break lines.

enter image description here

So, any tips here ?

1

4 Answers 4

2

Use the <pre> tag to keep the text formatted as you wish

<pre>
I am inside
   a pre
          Tag!
<pre>

Sign up to request clarification or add additional context in comments.

Comments

1

Add below CSS will resolve your issue. Thanks

p.jss98 {
   white-space: pre-wrap;
}

Comments

0

In Html you need to add <br /> to break lines.

"surchargeNoteText":"Please note <br /> that pre-payments will take up to 2 business <br /> days to be allocated to your reservation",

4 Comments

its not html, this text is coming from a json file
No problem. Then use let surchargeNoteText = surchargeNoteText.replace('\r \n', '<br />');
its actually rendering <br /> as a string, not interpretating as a html tag
I guess this is because the value is injected in the HTML document using innerText instead of innerHTML (before using the latter, make sure to read "Security considerations").
0

Use &nbsp; instead of \r and <br/> instead of \n. In HTML, \r and \n doesn't works.

2 Comments

its not html. its a dynamic text which comes from a json file
@vbotio HTML doesn't render these tags you are getting from JSON. You can replace them in JS before appending it in HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.