<div class="class1">
Parent element
<span class="class2">Child element</span>
</div>
If 'class2' exists under 'class1', then I want to delete 'class1'. How could I do that?
<div class="class1">
Parent element
<span class="class2">Child element</span>
</div>
If 'class2' exists under 'class1', then I want to delete 'class1'. How could I do that?
If you are using jQuery
$('.class1').each(function() { // Loop through all 'class1'
if($('.class2', this).length) // If class1 contains 'class2'
$(this).remove(); // Delete it!
});
If you are using pure Javascript
document.querySelectorAll('.class1').forEach(function(element) {
if(element.querySelector('.class2') !== null) {
element.parentNode.removeChild(element);
}
});
Working demo: https://jsfiddle.net/d6r6p68k/