Reputation: 2297
I'm trying to remove a specific div if a separate div is empty. Here's what I'm using:
$(document).ready(function () {
if ('#leftmenu:empty') {
$('#menuTitleWrapper').remove();
$('#middlemenu').css({ 'right': '0', 'position': 'absolute' });
$('#PageContent').css({ 'top': '30px', 'position': 'relative' });
}
});
I think this is close but I can't figure out how to write the code to test of #leftmenu is empty. Any help is appreciated!
Upvotes: 127
Views: 244629
Reputation: 305
also you can use this :
if (! $('#leftmenu').children().length > 0 ) {
// do something : e.x : remove a specific div
}
I think it'll work for you !
Upvotes: 6
Reputation: 1118
I've encountered this today and the accepted answers did not work for me. Here is how I did it.
if( $('#div-id *').length === 0 ) {
// your code here...
}
My solution checks if there are any elements inside the div so it would still mark the div empty if there is only text inside it.
Upvotes: 3
Reputation: 6468
You can extend jQuery functionality like this :
Extend :
(function($){
jQuery.fn.checkEmpty = function() {
return !$.trim(this.html()).length;
};
}(jQuery));
Use :
<div id="selector"></div>
if($("#selector").checkEmpty()){
console.log("Empty");
}else{
console.log("Not Empty");
}
Upvotes: 12
Reputation: 717
If you want a quick demo how you check for empty divs I'd suggest you to try this link:
http://html-tuts.com/check-if-html-element-is-empty-or-has-children-tags/
Below you have some short examples:
Using CSS
If your div is empty without anything even no white-space, you can use CSS:
.someDiv:empty {
display: none;
}
Unfortunately there is no CSS selector that selects the previous sibling element. There is only for the next sibling element: x ~ y
.someDiv:empty ~ .anotherDiv {
display: none;
}
Using jQuery
Checking text length of element with text() function
if ( $('#leftmenu').text().length == 0 ) {
// length of text is 0
}
Check if element has any children tags inside
if ( $('#leftmenu').children().length == 0 ) {
// div has no other tags inside it
}
Check for empty elements if they have white-space
if ( $.trim( $('.someDiv').text() ).length == 0 ) {
// white-space trimmed, div is empty
}
Upvotes: 23
Reputation: 322452
You can use .is()
.
if( $('#leftmenu').is(':empty') ) {
// ...
Or you could just test the length
property to see if one was found.
if( $('#leftmenu:empty').length ) {
// ...
Keep in mind that empty means no white space either. If there's a chance that there will be white space, then you can use $.trim()
and check for the length of the content.
if( !$.trim( $('#leftmenu').html() ).length ) {
// ...
Upvotes: 304
Reputation: 13465
In my case I had multiple elements to hide on document.ready. This is the function (filter) that worked for me so far:
$('[name="_invisibleIfEmpty"]').filter(function () {
return $.trim($(this).html()).length == 0;
}).hide();
or .remove() rather than .hide(), whatever you prefer.
FYI: This, in particular, is the solution I am using to hide annoying empty table cells in SharePoint (in addition with this condition "|| $(this).children("menu").length".
Upvotes: 2
Reputation: 111
if (typeof($('#container .prateleira')[0]) === 'undefined') {
$('#ctl00_Conteudo_ctrPaginaSistemaAreaWrapper').css('display','none');
}
Upvotes: 1
Reputation: 93664
It depends what you mean by empty.
To check if there is no text (this allows child elements that are empty themselves):
if ($('#leftmenu').text() == '')
To check if there are no child elements or text:
if ($('#leftmenu').contents().length == 0)
Or,
if ($('#leftmenu').html() == '')
Upvotes: 41
Reputation: 19399
Try this:
$(document).ready(function() {
if ($('#leftmenu').html() === "") {
$('#menuTitleWrapper').remove();
$('#middlemenu').css({'right' : '0', 'position' : 'absolute'});
$('#PageContent').css({'top' : '30px', 'position' : 'relative'});
}
});
It's not the prettiest, but it should work. It checks whether the innerHTML (the contents of #leftmenu) is an empty string (i.e. there's nothing inside of it).
Upvotes: 4