Use jquery to remove a div with no children


How can I use jquery to remove a SPECIFIC div that has no children (at least no children that isn’t whitespace). E.g.

<div id="outer">
    some content
    <div id="removeme"> 

    some more content

Want to completely remove the div with id=”removeme”.


To remove the element with id equal to removeme:


To remove the element with id equal to removeme only if it is empty:


To remove all empty <div>s:


EDIT: If it’s not empty, but has whitespace:

if($.trim($("#removeme").text()) == "") {

Answered By – Sophie Alpert

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published