2

Is there a way to make a jQuery object evaluate to false when it's created if its length is 0?

Here's what I'd like to be able to do:

var Constructor = function (config) {

  this.obj = $(config.id) || null;

};

But that doesn't work -- if config.id is not passed or #id is not found, this.obj becomes an empty jQuery object that evaluates to true.

What I want is for this.obj to be null on creation (or to be a populated jQuery object if the arg/id exists) and to avoid having to check the length of the resulting jQuery object.

3
  • You could check against the length property of the object Commented Jul 14, 2014 at 14:30
  • this.obj = $(config.id).get(0) may do it, it gets the Javascript object from the jQuery object Commented Jul 14, 2014 at 14:34
  • I think there is only one way to check if element exist by checking length of its array. Commented Jul 14, 2014 at 14:43

4 Answers 4

2

You could wrap the .length test up in a jQuery function:

$.fn.nonEmpty = function() {
  return this.length && this;
};

Then:

this.obj = $(config.id).nonEmpty() || null;
Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't this.obj have a value of true if it's not empty?
@cantera no, because the JavaScript && operator returns the first non-false value without converting it to boolean. Thus, the return value will be either 0 or a non-empty jQuery object.
Thanks - I consider this the cleanest approach shown so far.
Sorry that comment above explains thing incorrectly; && returns its first operand (left side) if that value is "falsy", and the second operand (right side) otherwise.
Thanks for clarifying -- this is a very useful technique.
2

Just use its .length property:

var Constructor = function (config) {
  var elem = $(config.id);
  this.obj = elem.length ? elem : null;
};

Since jQuery returns an empty array if nothing is found, its length will be 0 which evaluates to false

If you want to avoid length lookup, you can do this:

var Constructor = function (config) {
  var elem = $(config.id);
  this.obj = elem[0] ? elem : null;
};

3 Comments

Updated my question -- I should have specified that checking the length in a separate step is what I'm trying to avoid.
Wasn't me - I'm only seeing the +2
@cantera I know who it is and I also know it wasn't you :) It has been reverted back though
0

To make check if id is passed - you can use:

var Constructor = function (config) {

  this.obj = $(config.id || null );

};

But because of $ returns a collection - you should check length of this collection by .length

var Constructor = function (config) {    
  var $collection = $(config.id);

  this.obj = $collection.length ? $collection : null;
};

4 Comments

I suppose he wanted this.obj to be null. Your code returns [] which is not what OP wants I suppose
Good idea, but that produces the same result. If I evaluate this.obj, it still returns true even if null is being wrapped.
@AmitJoki: Yes that's correct, I'd like this.obj to take the value null.
@cantera check out my answer then. It does what you want.
0

As per my comment, another short solution is this:

var check = $(config.id);
this.obj = check.get(0)==null ? null : check

Look here: http://jsfiddle.net/V3G6U/2/

2 Comments

Thanks - I like the syntax, but unfortunately it requires diving into the DOM twice to find the element.
Fixed, its a 2 liner now ;p.

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.