Sometimes it is useful to have an empty jQuery object, to be used either as a default value or as an initial value, when constructing a collection of items.
For an example, see here.
One way to do it would be to use a selector which is unlikely to match anything, like $('skdhjfksjdhfksjhdf')
, but this is obviously inelegant.
How can I get an empty jQuery object in elegant style ?
The :empty selector in jQuery is used to select empty elements. Parameter: The :empty selector contains single parameter empty which is mandatory and used to select an empty element which is an element without child elements or text.
The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.
Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.
empty() method will clear or remove the contents inside an element, including the child nodes that are nested inside the parent or the selected element. Whereas the jQuery . remove() method will remove the selected element along with its contents and other nested elements (or child elements) and its contents.
Starting with jQuery 1.4, a simple $()
will return an empty set. jQuery 1.4 release notes ("jQuery() returns an empty set").
For earlier versions, use $([])
Do you mean...
//just get jQuery... var foo = $(); //or just get the browser using jQuery... if($.browser.msie){ alert('You are using the blue e!'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With