How do you check if an element is empty?



How do you check if an element is empty?..

Answer / chaitanya

There are 2 ways to check if element is empty or not. We can check using ":empty" selector.

Hide Copy Code

$(document).ready(function(){

if ($('#element').is(':empty')){

//Element is empty

}

});

And the second way is using the "$.trim()" method.

Hide Copy Code

$(document).ready(function(){

if($.trim($('#element').html())=='') {

//Element is empty

}

});

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More jQuery Interview Questions

What are selectors in jquery mean ?

0 Answers  


How do you add an HTML element in DOM tree?

0 Answers  


Difference between javascript and jquery? : jQuery Mobile

0 Answers  


Explain the each() function?

0 Answers  


Is it possible that jquery html work for both html and xml document?

0 Answers  


Consider a scenario where things can be done easily with javascript, would you still prefer jQuery?

1 Answers  


Explain event.preventdefault? : jquery mobile

0 Answers  


Define data paremeter of jquery ajax method?

0 Answers  


How can events be prevented from stopping to work after an ajax request?

0 Answers  


What is the difference between onload() and document.ready()?

0 Answers  


What is jquery toggle function?

0 Answers  


How to execute jQuery code after the DOM is ready?

0 Answers  


Categories