Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip all html tags with jquery?

Tags:

jquery

How to strip out all html tags in jquery. the function like strip_tags in PHP

eg: there is some contents as the following:

 <div id='test'>This is <b>bold</b> and this <div>is</div> <i>italic</i>.</div>

Now i want to change it to <div id='test'>This is bold and this is italic.</div>

like image 593
user1345545 Avatar asked Dec 21 '22 22:12

user1345545


1 Answers

Use .text():

$('#test').text($('#test').text());

I used .text() twice because using .html() to set the contents will cause strings with lesser-than and greater-than signs to be rendered incorrectly:

Hello &lt;foo&gt; // Set with .text()
Hello <foo>       // Set with .html()

If you used .html(), then <foo> would become a tag.

like image 109
Blender Avatar answered Jan 07 '23 00:01

Blender