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\') {
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 ) {
// ...
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");
}
if (typeof($('#container .prateleira')[0]) === 'undefined') {
$('#ctl00_Conteudo_ctrPaginaSistemaAreaWrapper').css('display','none');
}
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 !
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".
if($('#leftmenu').val() == "") {
// statement
}