Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  63] [ 5]  / answers: 1 / hits: 121853  / 12 Years ago, sat, january 26, 2013, 12:00:00

Possible Duplicate:

Checking if an html element is empty with jQuery






I have an empty div like this:



<div id=cartContent></div>


I need to check if it's empty. If it's empty it needs to return true and then some

elements are added. If not it returns false and some other function executes. What's the best way to check if there are any

elements in the div?



Thanks


More From » jquery

 Answers
15

You can use native JavaScript:


if document.getElementById('cartContent').innerHTML === "" {

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 ) {

You can use $.trim() to remove whitespace (if that's what you want) and check for the length of the content:


if( !$.trim( $('#leftmenu').html() ).length ) {

[#80608] Friday, January 25, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;