Reputation: 55
I am trying to implement a certain functionality on a website, and I need to check for anything written in-between <ul></ul>
tags. And when there isn't any, text should be appended between the tags
So far I've tried doing conditional statements but it seems the statements are just being overlooked when I debugged them.
Here is the code that I have tried:
if($('ul.list-group').length > 0 === false) {
$('ul.list-group').append('Drag a page inside or below the box to create a child page');
}
Any help is greatly appreciated.
Upvotes: 0
Views: 64
Reputation: 67187
Try to collect the text content of the target element and check its length,
var elem = $('ul.list-group');
if(!elem.text().length) {
elem.text('Drag a page inside or below the box to create a child page');
}
As epascarello pointed out, An UL cannot have a text node as its immediate child. Not exactly, It can. But it is considered as an invalid html. The better approach would be,
var elem = $('ul.list-group');
if(!$("li", elem).length) {
elem.html('<li>Drag a page inside or below the box to create a child page</li>');
}
Upvotes: 1
Reputation: 92854
To find all ul.list-group
elements that are empty - they don't have child elements or text:
$('ul.list-group:empty').text('Drag a page inside or below the box to create a child page');
https://api.jquery.com/empty-selector/
Upvotes: 0