2

I have an <div> with several child's inside it which is removed one after another and I want to check when all elements inside are removed .

I tried checking with if($('#data').html()=="") but its not working, probably because of white spaces and tab spaces.

Keeping track of each and every elements and checking can be done but I am sure it will be worse idea for this simple task so is there any easy way to do this ?

Shub
  • 2,686
  • 17
  • 26

6 Answers6

8

if($('#data').children().length > 0) is the best solution in my opinion or shorter if($('#data').children().length)

API

schnawel007
  • 3,982
  • 3
  • 19
  • 27
1

Try to use .find('*'), the reason why we are passing * inside it is, .find() cannot be called without parameters as .children() do. But the better option would be using .children() at this context.

if($('#data').find('*').length) {
  //its empty
}

or you can use :empty selector,

if($('#data').is(':empty')) {
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

You can use children() function to get all child elements http://api.jquery.com/children/

and check the array length

if($('#data').children().length){
   //do smth
}
knizhnikov
  • 299
  • 1
  • 7
0
if ($('#data').is(':empty')){
  //do something
}

if($.trim($("selector").html())=='')

See more: How do I check if an HTML element is empty using jQuery?

Community
  • 1
  • 1
Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
0

Use .children() in jquery to find the children is present or not

   if($('#data').children().length > 0) {


    }
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
0

If all your children are elements of some kind try to use the following: $('data').children().length == 0

note that white spaces and characters are not considered children

you can check if there is text content using: $('data').text().length ==0

Note That new line are also characters

Kfir Erez
  • 3,280
  • 2
  • 18
  • 17