Alex Alex - 2 years ago 147
HTML Question

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.


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


<div id="text">
<p> paragraph </p>
<div> should be stripped </div>

Answer Source

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();

Try it out here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download