Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my "<br />" tags getting converted to "&lt;br /&gt;"?

I have HTML data stored in database which I wish to display as is. It keeps converting
tags to &lt;br /&gt; 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.

like image 396
jini Avatar asked Sep 08 '11 19:09

jini


4 Answers

&lt;br /&gt; == <br /> You just need to Decode the output to get back the original HTML.

Use javascript unescape function

like image 190
Icarus Avatar answered Nov 13 '22 12:11

Icarus


You can also replace the chars with the actual <br/> tag.

myString.replace(/&lt;br&gt;/g, '<br/>')
like image 36
Juan Pablo Ugas Avatar answered Nov 13 '22 12:11

Juan Pablo Ugas


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.

like image 7
Senad Meškin Avatar answered Nov 13 '22 13:11

Senad Meškin


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.

like image 2
Quentin Avatar answered Nov 13 '22 13:11

Quentin