Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery (almost) equivalent of PHP's strip_tags()

Is there a jQuery version of this function?

string strip_tags( string $str [, string $allowable_tags ] )

strip all tags and content inside them from a string except the ones defined in the allowable tags string.

like:

var stripped = strip_tags($('#text').html(), '<p><em><i><b><strong><code>'); 

from:

<div id="text">   <p> paragraph </p>   <div> should be stripped </div> </div> 
like image 469
Alex Avatar asked Apr 08 '11 23:04

Alex


2 Answers

To remove just the tags, and not the content, which is how PHP's strip_tags() behaves, you can do:

var whitelist = "p"; // for more tags use the multiple selector, e.g. "p, img" $("#text *").not(whitelist).each(function() {     var content = $(this).contents();     $(this).replaceWith(content); }); 

Try it out here.

like image 68
karim79 Avatar answered Sep 29 '22 08:09

karim79


To remove all tags you can use

$('<div>Content</div>').text() 
like image 43
timfjord Avatar answered Sep 29 '22 07:09

timfjord