Skip to content
Advertisement

Find if classes child class has empty innerHTML in javascript/jquery

I am trying to figure it out whole day already and nothing seems to work, so I would really appreciate help. Here is simplified example of what I have in HTML:

JavaScript

Result is:

JavaScript

What I would like to achieve is to show middle-div only if small-div has any content, so the result would be:

JavaScript

I have tried this approach:

JavaScript

and the result looks like this:

JavaScript

I have also tried this:

JavaScript

But that didn’t do anything and console.log(small.innerHTML); showed undefined.

Can someone help me figure out how to do this correctly?

Advertisement

Answer

I’d find all the .small-div elements inside .middle-div elements inside #big-div and if the don’t have any child nodes, add a class to the .middle-div that hides it:

JavaScript

Live Example (hides the elements after 800ms):

JavaScript
JavaScript
JavaScript

If you want to allow for a single node that has whitespace and still consider that div “empty”, you could use:

JavaScript

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement