I have HTML data stored in database which I wish to display as is. It keeps converting
tags to <br />
which is a behavior I do not want. I have tried playing with javascript replace and still I am unable to convert it to regular HTML.
var venueaddress = msg.result[0].venueaddress;
var venueaddress2 = venueaddress.replace("[newline]", "<br />");
alert(venueaddress2); //shows <br />
$("#venueaddress").text(venueaddress2); //lets now display it on the browser
<li><h3>Venue Address</h3><p><strong> <span id="venueaddress"></span> </strong></p></li>
However when it renders on browser, it has the <br /> and there fore there is no line break.
<br />
== <br />
You just need to Decode the output to get back the original HTML.
Use javascript unescape function
You can also replace the chars with the actual <br/>
tag.
myString.replace(/<br>/g, '<br/>')
Your problem is with
$("#venueaddress").text(venueaddress2);
you should use
$("#venueaddress").html(venueaddress2);
Text will encode any html character and will display it in span as encoded, html will not.
Presumably because when you are inserting it into the DOM, you are inserting it as text and not as HTML.
Since you haven't show the code you are using to do that, it is hard to say for sure, or to say what the best way to change it so it expects HTML would be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With