Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery empty selector (null selector)

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 ?

like image 708
Peter Kaleta Avatar asked Aug 04 '09 16:08

Peter Kaleta


People also ask

Is jQuery selector empty?

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.

What is jQuery empty?

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.

How check string is empty in jQuery?

Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.

What is the difference between Remove and empty in jQuery?

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.


2 Answers

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 $([])

like image 124
itsadok Avatar answered Sep 22 '22 21:09

itsadok


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!'); } 
like image 41
scunliffe Avatar answered Sep 23 '22 21:09

scunliffe