Categories
JavaScript Answers

How to check if a div element is empty with JavaScript?

Spread the love

Sometimes, we want to check if a div element is empty with JavaScript.

In this article, we’ll look at how to check if a div element is empty with JavaScript.

How to check if a div element is empty with JavaScript?

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

For instance, we write:

<div></div>

to add an empty div.

Then we write:

const isEmpty = document.querySelector('div').innerHTML === "";
console.log(isEmpty)

to select the div with querySelector.

Then we check if its innerHTML property is an empty string.

If it is, then the div is empty.

Since, isEmpty logs true, the div is empty.

Conclusion

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *