Remove element from JS injected HTML

This doesn’t seem to be working? Any thoughts?

Remove [0] as parent() will give you a jquery element while [0] will give you plain DOM element which doesn’t have remove() method.

Also, you need to store the reference of jquery element that you are going to manipulate.

var description = '<div class="description">Out of P<p>This is a <a href="#">test</a></p></div>';
var $description = $( description );
$description.find('a').parent().remove();
$( 'body' ).append( $description );

Demo

var description = '<div class="description">Out of P<p>This is a <a href="#">test</a></p></div>';
var $description = $( description );
$description.find('a').parent().remove();
$( 'body' ).append( $description );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Leave a Comment