How can I use jquery to remove a SPECIFIC div that has no children (at least no children that isn\'t whitespace). E.g.
some cont
Here is if empty with children then you can point at children and then remove a parent, this dont look at whitespaces it just remove if empty
so like; if li is empty it removes #removeme
if (!$("#tabs ul li").length) $('#tabs').remove();
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();
}
I went with:
$('#outer > div').filter(function (index) {
return $(this).children().length < 1;
}).remove();
This says:
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.
You can also use:
$('div:empty').remove();
http://docs.jquery.com/Selectors/empty
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()
});
I think you want this:
$('div#outer div:empty').remove();
It will remove all the empty divs inside the div#outer node