Reputation: 385
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
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
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