Using an if statement to check if a div is empty

Jquery

Jquery Problem Overview


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!

Jquery Solutions


Solution 1 - Jquery

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 ) {
    // ...

Solution 2 - Jquery

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() == '')

Solution 3 - Jquery

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
}

Solution 4 - Jquery

You can extend [jQuery][1] [1]: https://jquery.com/ 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");
}

Solution 5 - Jquery

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 !

Solution 6 - Jquery

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).

Solution 7 - Jquery

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

Solution 8 - Jquery

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.

Solution 9 - Jquery

if (typeof($('#container .prateleira')[0]) === 'undefined') {
    $('#ctl00_Conteudo_ctrPaginaSistemaAreaWrapper').css('display','none');
}

Solution 10 - Jquery

if($('#leftmenu').val() == "") {
   // statement
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike MullerView Question on Stackoverflow
Solution 1 - Jqueryuser113716View Answer on Stackoverflow
Solution 2 - JqueryDavid TangView Answer on Stackoverflow
Solution 3 - JqueryDanView Answer on Stackoverflow
Solution 4 - JqueryArifView Answer on Stackoverflow
Solution 5 - JqueryZeinab_NsView Answer on Stackoverflow
Solution 6 - JqueryReidView Answer on Stackoverflow
Solution 7 - JqueryPatrik AffentrangerView Answer on Stackoverflow
Solution 8 - JqueryRicardo GreenView Answer on Stackoverflow
Solution 9 - JqueryTadeu LuisView Answer on Stackoverflow
Solution 10 - JqueryDamnView Answer on Stackoverflow