NZMAI NZMAI - 3 months ago 15
HTML Question

Hide a div if paragraph does not contain any text

I'm doing a project using Wikipedia API. Typing a word in seach bar displays a list of items. Like in the picture below.
Image one
When a user hover over "more" div an additional content is dispalyed below. like that.
Image two

However some items in blue color do not containany information, that is, empty. So i want for those items which do not contain any additional information do not show "more" div. So i have written a code. But unfortunately it does not work.

if ($('.list-container>div.titleDesc>p').is(':empty')) {
$(".show-more").hide();
}


Cannot figure out what is wrong so will appreciate any help. If you need more information please refer to my project in Codepen.

Answer

jQuery :empty() is a selector; hence, it works on jQuery elements. So, you need to update this line:

$(this).text().is(':empty')

With this line:

$(this).is(':empty')

Here is a Working Demo, I have just updated this JS line, and add a container div element.

Comments