Given a string of html code, how can I go through every tag and remove ones that is not in my whitelist (in JQuery)?
<pre><code>var whitelist = ['a','div','img', 'span'];
</code></pre>

<p>Given a block of HTML code, I want to go through <strong>every single tag</strong> using JQuery</p>

<p>Then, if that tag is NOT in my whitelist, <strong>remove it and all its children.</strong></p>

<p>The final string should now be sanitized.</p>

<p>How do I do that?</p>

<p>By the way, this is my current code to remove specific tags (but I decided I want to do whitelist instead)</p>

<pre><code>        var canvas = '&lt;div&gt;'+canvas_html+'&lt;/div&gt;';
        var blacklist = ['script','object','param','embed','applet','app','iframe',
        'form','input', 'link','meta','title','input','button','textarea'
        'head','body','kbd'];
        blacklist.forEach(function(r){
            $(canvas).find(r).remove();
        });
        canvas_html = $(canvas).get('div').html();
</code></pre>
