how to check if a div is empty
how to check if a div is empty
Hello I have a div that has some content inside of it but when its empty I want it to say something like div is empty
heres my code
<div class="results">Hello</div>
Possible duplicate of Check if the content of a DIV is empty in jQuery
– Developer kt
Jul 1 at 4:14
Please take the time to comment on the answers you received.
– connexo
Jul 1 at 4:24
7 Answers
7
CSS has a pseudo class :empty exactly for this purpose. Use that to find the right divs, and use CSS pseudo element ::before to insert the text you want to display in empty divs:
:empty
div
::before
.results:empty::before {
content: "I feel so empty";
color: #999;
}
<div class="results">Hello</div>
<div class="results"></div>
You can check the text content of <div> by .text(), use trim() to remove spaces.
<div>
.text()
trim()
var isEmpty = $('.results').text().trim() === '';
console.log( isEmpty );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="results">Hello</div>
You can create your own. You can get all the contents of a div with $.html(), so check if $("myDiv").html() == "" and this is your answer.
You can make it as a standalone function
function isDivEmpty(elementId){
return $(elementId).html() == "";
}
or extend jQuey to have access to it anywere.
$.fn.isEmpty = function(){
return $(this).html() == "";
}
// usage
alert($("#test").isEmpty()); // alerts true or false
Using plain javascript
var isEmpty = document.getElementsByClassName('results').innerHTML === "";
using jquery
var isEmpty = $(".results").html() === "";
You can use jquery like this:
if ($('.results').is(':empty')){
// insert code here
}
Edit: #div is a placeholder.
#div
Edit 2: Changed #div to .results
#div
.results
if ($('.results').is(':empty')){
console.log('empty');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="results">Hello</div>
<div class="results"></div>
<div class="results">World</div>
OP's code does not have an element with the
id div.– connexo
Jul 1 at 4:12
id
div
yes im using id div as a placeholder
– jam
Jul 1 at 4:14
OP might have many divs on the same page. All have
.results, but none has an id. And now? Added that case as a snippet to your answer. Please check to understand the problem of your approach.– connexo
Jul 1 at 4:15
.results
id
@connexo Thank you, I'll improve on my answering in the near future.
– jam
Jul 1 at 5:49
Another approach to check if a div is empty:
div
var isEmpty = !$(".results")[0].hasChildNodes();
console.log(isEmpty);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="results">Bello</div>
you can find the working solution here
https://jsfiddle.net/06nLec5t/8/
used id to identify the empty div name, or you can exclude that.
div
the one who marked the answer down, please mark the concern for marking it down, Hope it will help my answering skills in future.
– Rnayak
Jul 1 at 5:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Just a simple google search yielded me this html-tuts.com/…
– Vineet
Jul 1 at 4:08