How do I check if an HTML element is empty using jQuery

0 votes

I'm trying to call a function only if an HTML element is empty, using jQuery.

Something like this:

if (isEmpty($('#element'))) {
    // do something
}

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

Aug 25, 2020 in Java-Script by kartik
• 37,520 points
3,641 views

1 answer to this question.

0 votes

Hello @kartik,

Try:

if ($('#element').is(':empty')){
  //do something
}

 If you would like to ignore invisible elements such as spaces and line breaks and make the implementation more consistent you can create a function (or just use the code inside of it).

  function isEmpty( el ){
      return !$.trim(el.html())
  }
  if (isEmpty($('#element'))) {
      // do something
  }

You can also make it into a jQuery plugin.

Hope it helps!!
Thank you!

answered Aug 25, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
2 answers

How can I set focus on an element in an HTML form using JavaScript?

Hi Kartik, try the following script <script>  (window.onload = ...READ MORE

answered Sep 24, 2020 in Java-Script by Okugbe
• 280 points
1,848 views
0 votes
1 answer

How can I check the existence of an element in jQuery?

Hello @ Arpit In JavaScript, everything is 'truthy' or ...READ MORE

answered Sep 8, 2020 in Java-Script by Niroj
• 82,840 points
844 views
0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the quickest and most effective method to determine whether a JavaScript array has a value? The only method I am aware of is as follows: function contains(a, obj) { ...READ MORE

Sep 23, 2022 in Java-Script by Abhinaya
• 1,160 points
747 views
0 votes
1 answer

How do I test for an empty JavaScript object?

Hello @kartik, Try this: var obj = {}; return Object.keys(obj).length; Hope ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
594 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How do I check if an element is hidden in jQuery?

Hello @kartik,  You can use CSS: class .hide { ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
979 views
0 votes
1 answer

How do I select an element with its name attribute in jQuery?

Hello @kartik, You can use: jQuery('[name="' + nameAttributeValue + ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,840 points
1,059 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP