28

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 ?

5
  • Where would you need an empty selector? Commented Aug 4, 2009 at 16:18
  • Interesting, what's the point? Commented Aug 4, 2009 at 16:19
  • What do you mean? Perhaps further explanation and complete sentences would help garner an answer. Commented Aug 4, 2009 at 16:42
  • Really not bad enough to warrant two down votes. Commented Oct 14, 2010 at 3:13
  • I'm using empty jQuery object when I need to make an operation on an undetermined number of nodes (where zero is an acceptable solution). It can be to add a class on several inputs when validating a form or to filter a list of items. Commented Sep 4, 2016 at 9:44

2 Answers 2

34

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.