Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: stop rendering HTML code inside div [duplicate]

If i write something like below inside textarea

<strong>test</strong>

inside textarea and then display it inside a div. It shows the text in bold letter instead of showing the html code as it is. So how can i avoid rendering html code. I just want to show as it is the text was entered by user.

like image 201
Manish Kumar Avatar asked Aug 24 '26 11:08

Manish Kumar


2 Answers

You haven't shown us any code, but since you have tagged this jQuery I'll assume you have something like:

$('div').html(
     $('textarea').val()
);

Use the text method instead, so what the user has typed is treated as text instead of HTML.

$('div').text(
     $('textarea').val()
);

See a demonstration.

like image 133
Quentin Avatar answered Aug 26 '26 02:08

Quentin


You need to escape the < and > characters to their HTML entities.

&lt;strong&gt;test&lt;/strong&gt;

JSFiddle

like image 28
epascarello Avatar answered Aug 26 '26 01:08

epascarello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!