tags:

views:

13846

answers:

7

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"> 


    </div>
    some more content
</div>

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

A: 

I couldn't find a selector that ignores text nodes, so this is the quickest/dirtiest code snippet I could come up with.

$("#header").each(function() { 
    if($(this).children().length < 1) 
        $(this).remove() 
});
Stuart Branham
could you explain this for us?
Here, we're selecting a div that is not a parent. (does not have child elements)See the jQuery selectors documentation: http://docs.jquery.com/Selectors
Stuart Branham
i don't want to select and remove EVERY div that has no children
You asked how to use jquery to remove a div that has no children. In that case, just use $("#removeme").remove()
Stuart Branham
that doesnt look like its checking to see whether is has children
+2  A: 

You can also use:

$('div:empty').remove();

http://docs.jquery.com/Selectors/empty

rmurphey
so $('div#removeme:empty').remove() gets just that node?
Yes, only if it has no child elements or inner text.
Stuart Branham
and if it has inner text (albeit whitespace)?
then no. It won't select it or remove it.
Jonathan
+27  A: 

To remove the element with id equal to removeme:

$("#removeme").remove();

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

$("#removeme:empty").remove();

To remove all empty <div>s:

$("div:empty").remove();

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

if($.trim($("#removeme").text()) == "") {
  $("#removeme").remove();
}
Ben Alpert
I think he's just referring that div by giving it an id, but he actually wants to remove all empty divs on the page... thats just my guess.
Luca Matteis
No, Ben is correct. Want to remove a specific div if its empty
Well, I included all the interpretations of the question I could think of. : P
Ben Alpert
hmmm, actually this doesnt work. empty whitespace counted as children?
I would guess that :empty will work if you change the div markup to be self-closing: <div id="removeme"/>
Stuart Branham
I'm sure it would, but if i could change the div to be self closing I wouldn't need to remove via jquery later :)
Then try :not(:parent). :empty isn't working because it thinks there is some inner text in that div.
Stuart Branham
Tried $('#removeme:not(:parent)').remove() - no good, same problem as :empty I presume
the last code chunk (appended with a .remove()) bombs out.
Changed it a bit, now try?
Ben Alpert
No, that removed the node even when it did have non-whitespace children
Okay, last try before I give up.
Ben Alpert
Congratulations. Got it on the last go! Thanks Ben.
if only wants to remove or change the id what we can do? not entire div
Behrang
A: 

I think you want this:

$('div#outer div:empty').remove();

It will remove all the empty divs inside the div#outer node

Luca Matteis
A: 

Could be in effect "Fader out"?

Deniw
A: 

$(document).ready(function() { $('div:empty').remove(); });

looking to use this but it wont work! I look in the source code and it's still there, any ideas? Thanks

Alex Blundell
A: 

I went with:

$('#outer > div').filter(function (index) { 
    return $(this).children().length < 1; 
}).remove();

This says:

  • give me all the div children of #outer
  • use filter to get rid of any which have child nodes
  • remove anything we still have selected

Sadly, this will remove the div if it contains text, which is probably not what the original poster would have wanted. Plain text doesn't count as a child.

Jonathan