You have a couple issues with your code.
Typically, when someone does $( this ).find()
, this
is refering to an element like a <div>
, <button>
, whatever. In your code, this
is referring to the window
. While that appears to work, here is the typical way to search the whole page with jQuery, $( 'html:lang(en-US) .house-senate .featured-block' )
.
Your .featured-block
is not actually empty. It has text nodes in it, so is( ':empty' )
will fail.
alert( $( 'html:lang(en-US) .house-senate .featured-block' ).is( ':empty' ) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html class="js grunticon objectfit object-fit flexbox" lang="en-US">
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block">
</div>
</div>
</html>
Make sure there is no white-space between the opening and closing DIV tags.
alert( $( 'html:lang(en-US) .house-senate .featured-block' ).is( ':empty' ) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html class="js grunticon objectfit object-fit flexbox" lang="en-US">
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block"></div>
</div>
</html>
Even with the fix above, your code could still be buggy. If there's more than one html:lang(en-US) .house-senate .featured-block
element on the page, and at least one of them is empty, then $( 'html:lang(en-US) .house-senate .featured-block' ).is( ":empty" )
will return true. Then, $(this).find(".house-senate").css("display", "none");
would hide every .house-senate
on the page. That is, if the $(this)
issue was resolved to work properly like $( '.house-senated' ).css( 'display', 'none' );
.
alert( $( 'html:lang(en-US) .house-senate .featured-block' ).is( ":empty" ) );
if ( $( 'html:lang(en-US) .house-senate .featured-block' ).is( ":empty" ) ) {
$( '.house-senate' ).css( 'display', 'none' );
}
.house-senate {
padding: 2rem;
border: 1px solid rebeccapurple;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html lang="en-US">
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block"></div>
</div>
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block">Not Empty</div>
</div>
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block"><!-- I'm still empty. --></div>
</div>
</html>
There's also a problem with:
if ( $( 'html:lang(en-US) .house-senate .featured-block' ).is( ":empty" ) ) {
$( '.house-senate' ).css( 'display', 'none' );
}
The issue here is that there is no direct correlation between the check and what's being hidden. What you want to be doing, and I'm sure this is your intention, is "Only hide .house-senate
when its child .featured-block
element is empty."
What you need to do is get all the empty .featured-block
elements, then for each empty element found, move up the DOM tree from that element to .house-senate
and hide it. That way, there's a tight cooupling happening between what is empty and what is hidden.
jQuery always operates on collections (think of an array). As a result, jQuery methods will "magically" work when a selector returns one or many elementsl without you needing to perform loops. Because of this, your code could be simplified to:
$( '.featured-block:empty' ).parent().hide();
.house-senate {
padding: 2rem;
border: 1px solid rebeccapurple;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html lang="en-US">
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block"></div>
</div>
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block">Not Empty</div>
</div>
<div class="focustop house-senate widget widget-cpac-depth -horizontal">
<div class="featured-block"><!-- I'm still empty. --></div>
</div>
</html>
Note: I'm not sure if html:lang(en-US)
and .house-senate
are neccessary to your selector. You may need to add them back in if you actually need to do something different for a different language or if .featured-block
exists outside of a .house-senate
element.