Peter Kaleta
Peter Kaleta

Reputation: 385

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 ?

Upvotes: 28

Views: 9059

Answers (2)

itsadok
itsadok

Reputation: 29352

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

Upvotes: 34

scunliffe
scunliffe

Reputation: 63676

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!');
}

Upvotes: 1

Related Questions