This creates an empty jQuery-object:

$([])

Update: In newer versions of jQuery (1.4+), you can use:

$()
Answer from Magnar on Stack Overflow
🌐
jQuery
api.jquery.com › jQuery.isEmptyObject
jQuery.isEmptyObject() | jQuery API Documentation
Description: Check to see if an object is empty (contains no enumerable properties).
🌐
jQuery
api.jquery.com › empty
.empty() | jQuery API Documentation
Description: Remove all child nodes of the set of matched elements from the DOM · This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element ...
🌐
GeeksforGeeks
geeksforgeeks.org › jquery-isemptyobject-method
JQuery | isEmptyObject() method | GeeksforGeeks
April 27, 2020 - This isEmptyObject() Method in jQuery is used to determines if an object is empty.
🌐
Javatpoint
javatpoint.com › jquery-isemptyobject-method
jQuery isEmptyObject() method - javatpoint
jQuery isEmptyObject() method with jQuery tutorial, methods, html and css, properties, examples of jQuery effects, selectors, traversing, events, manipulation, animation, html and more.
🌐
Jquery
forum.jquery.com › topic › jquery-creating-an-empty-jquery-object
Jquery
We cannot provide a description for this page right now
Find elsewhere
🌐
ItSolutionstuff
itsolutionstuff.com › post › -how-to-check-object-is-empty-or-not-in-jquery-javascript-example.html
How to Check Object is Empty or Not in JQuery? - ItSolutionstuff.com
January 7, 2023 - you can check your JavaScript OR jQuery object is empty or not, because we need to check many place our jQuery object is empty, null or undefined etc., So usually, we can check using $.isEmptyObject() as i explained as under.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty. function isEmptyObject(obj){ return JSON.stringify(obj) === '{}' } jQuery.isEmptyObject(obj); Related ReadingWhat Is the @ Symbol ...
Top answer
1 of 6
81

Your problem here is that there aren't "null" jQuery objects that can't be appended to and "empty" ones that can be. You've already identified several ways to create an empty one, and while there may be more, it doesn't matter - what you're trying to do simply won't do what you expect it to, no matter how you start off, because...

...append() modifies the DOM, not the jQuery object. And an empty jQuery object has no DOM! Tomalak had the right idea, though you may not have understood it.

I'm going to make three observations about jQuery that you're probably already aware of, but which are useful in understanding the remainder of this answer and therefore may benefit future readers:

  1. A jQuery object is fundamentally a set of DOM elements.
  2. Some jQuery methods operate on the set, and some operate on the nodes in the set.
  3. Some jQuery methods operate only on (or retrieve information only from) the first node added to the set.

With these three observations in mind, let's consider your example:

// 1. create an empty set somehow (doesn't matter how)
var $new = $();

// 2. iterate over something
$.each( ..., function( ... )
{
  // 3. construct a DOM fragment from HTML, and append it
  $new.append("<sometag>"); 
});

// 4. try to display HTML corresponding to the jQuery object
alert($new.html());  

In this example, step #3 will fail to do anything useful because the purpose of append() is to take whatever DOM elements it is given and append them as children to each DOM element in the set. Since $new is an empty set, there are no elements to append to, and... nothing happens. Step #1 is working just fine - but by creating a set without any DOM elements in it, you've set yourself up to fail when you try to use methods that exist to modify the DOM! Reference Observation #2... to modify the set, you need to call a method that actually operates on the set.

Ok, so let's say we use the set modification method add() instead:

  $new = $new.add("<sometag>");

Now we're good, right? Each pass through the loop will create a new jQuery object consisting of the previous set + a newly-created element. Well...

...this will just cause step #4 to do something odd. See, html() is one of those methods I mentioned in Observation #3 - it operates only on the first element in the set. So instead of a sequence ("<sometag><sometag><sometag>...") you're only gonna get a HTML representation of the element created on the first pass through the loop ("<sometag>"). Except... You're not even going to get that, because html() creates a representation of the element's children - so what you're really going to end up with is ""...

This was all just a big disappointment: you started off working with the set (Step #1), then tried to operate on the DOM (Step #3), and finished up by trying to pull data from one DOM element (that didn't exist) (and wouldn't have had any data if it did) (Step #4).

The solution you finally came up with isn't terrible - you're essentially opting to add one root element to the set starting out, and operate entirely on this DOM fragment until you're done adding new elements and are ready to operate on them. You've omitted it from your revised example, but html() would work too, since it would simply dump the contents of your root element.

But just to be complete, I'll give you a final example that works on the set, starting with an empty jQuery object:

var $new = $();
$.each( [1, 2, 3, 4], function(i, v)
{
  $new = $new.add("<div>");
});

alert($new.length == 4); // true

// alerts: <div></div><div></div><div></div><div></div>
alert($("<div>").append($new).html()); 
2 of 6
24

Document Fragments are perfect for this; creates an empty object ready for appending. :)

$(document.createDocumentFragment())
🌐
W3Schools
w3schools.com › jquery › html_empty.asp
jQuery empty() Method
jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties ... The empty() method removes all child nodes and content from the selected elements.
🌐
jQuery
api.jquery.com › empty-selector
:empty Selector | jQuery API Documentation
Finds all elements that are empty - they don't have child elements or text. Ajax · Global Ajax Event Handlers · Helper Functions · Low-Level Interface · Shorthand Methods · Attributes · Callbacks Object · Core · CSS · Data · Deferred Object · Deprecated ·
🌐
GitHub
github.com › jashkenas › underscore › issues › 1715
Inconsistent results when passing empty jQuery object to _.isEmpty · Issue #1715 · jashkenas/underscore
I am noticing some strange behavior when running _.isEmpty on an empty jQuery object. At first, _.isEmpty returns false, but if I add an empty collection to it the collection, it stays empty, but _.isEmpty returns true after that. Here i...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
jQuery object has empty properties, but shows up when used console logging particular property
October 18, 2022 - How does the object property appear empty, but in reality isn’t · Because you’re logging the wrapper jQuery builds when you run the function $ (which is an alias for new jQuery.fn.init("#anElement") IIRC) · This topic was automatically closed 182 days after the last reply.
🌐
GitHub
github.com › jquery › api.jquery.com › issues › 1192
Document jQuery.isEmptyObject doesn't take symbols into account · Issue #1192 · jquery/api.jquery.com
December 26, 2020 - isEmptyObject: function( obj ) { var keys = Object.keys(obj); // 看浏览器是否支持 Symbol if (typeof Symbol !== "undefined") { keys = keys.concat(Object.getOwnPropertySymbols(obj)); } return keys.length === 0; }
Published   Jun 06, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › jquery-empty-method
jQuery empty() Method | GeeksforGeeks
July 10, 2023 - This defines accessing elements in the DOM tree. Syntax: $(se ... The empty() method is an inbuilt method in jQuery that is used to remove all child nodes and their content for the selected elements.