Search code examples
jquerydomnoscript

How to remove noscript tag wrapper with jquery?


I want to remove noscript tag that is wrapping round an image:

<a href="some_image.jpg">
 <noscript>
  <img src="some_image.jpg">
 </noscript>
</a>

I tried unwrap(), but it doesn't work inside noscript, next I tried the html() method:

$('a').html(function(index, oldhtml){
   return oldhtml.replace(/\<noscript\\?>/g, '');
});

Though the tag is removed, it produces a string instead of DOM:

<a href="some_image.jpg">
 "
  <img src="some_image.jpg">
 "
</a>

How to remove the noscript tag wrapper while keeping the img element untouched?


Solution

  • Try something like - it is in fact a very crude hack

    jQuery(function($){
        $('a:has(noscript)').html(function(){
            return $(this).find('noscript').text()
        })
    })
    

    Demo: Fiddle