0

I have an object returned from REST service, one property of this object is supposed to be multiple lines of text, sometimes it's returned like the following in html (when viewing in dev tools)

 "this point will be as follows: ↵↵1- point info here ↵2- point2 info here

Sometimes it's displayed in html as:

this point will be as follows: 

1- point info here
2- points2 info here

But in fact, on the page, all rendered on one line.

When I add this text to an html element using the following code:

$("#elementId").html(myValue); //value is the text I mentioned above.

Where elementId is a paragraph tag. It's not setting the value as multilines of text.

Note: I am getting the values from SharePoint, I am still new to it. Any idea why .html() is not working right, specially in the second case?

Thanks

Jacky
  • 393
  • 1
  • 3
  • 13

1 Answers1

2

Do a Replace

myValue = myValue.replace(/↵/g, "<br/>");

var myString = "this points will be as follows: ↵↵1- point info here ↵2- point2 info here";
myString = myString.replace(/↵/g, "<br/>")

console.log(myString);
abc123
  • 17,855
  • 7
  • 52
  • 82
  • Thanks, but what are these symbols? – Jacky Dec 08 '16 at 19:33
  • those are line break symbols, likely generated by `\n` or `\r\n` depending on windows or unix – abc123 Dec 08 '16 at 19:33
  • I am doing a replace like the following: `$("#elementId").html(myValues.replace(/↵/g, "
    "));` but it's not working
    – Jacky Dec 08 '16 at 19:35
  • it works when I try it directly in developer tools, but my code above doesn't work – Jacky Dec 08 '16 at 19:39
  • should I store the value after the replace, then assign it to my html? – Jacky Dec 08 '16 at 19:41
  • @Jacky I would just do the replace, re-storing the data after the replace would be redundant. It would be better to run a 1 time script to update all the data. Then not use the replace at all. – abc123 Dec 09 '16 at 15:58