Skip to main content

Bug Tracker

Side navigation

#3842 closed feature (worksforme)

Opened January 14, 2009 10:46AM UTC

Closed November 19, 2010 02:26AM UTC

Add an exists() function

Reported by: kbwood Owned by:
Priority: minor Milestone: 1.3.1
Component: core Version: 1.3
Keywords: Cc:
Blocked by: Blocking:
Description

Rather than use the construct $('selector').length > 0 it would be more readable and require less knowledge of the inner workings to able to use $('selector').exists().

As an enhancement you could pass in an expected number of occurrences and return true only if an exact match. With no parameter it returns true if any number exist.

The code is:

exists: function( size ) {

return (size ? this.length == size : this.length > 0);

},

Attachments (0)
Change History (8)

Changed January 14, 2009 02:24PM UTC by Thasmo comment:1

That sounds good - nice idea!

Changed January 15, 2009 02:17PM UTC by john comment:2

component: unfilledcore
milestone: 1.31.3.1
type: enhancementfeature
version: 1.2.61.3

Changed January 21, 2009 02:53PM UTC by balazs.endresz comment:3

I use a similar plugin but that returns the current jQuery object or null, so it can be chained and throws an error if there isn't any match:

return (size ? this.length == size : this.length > 0) ? this : null;

And, of course, it still can be used in a conditional statement.

Though ´if( $('selector').length )´ is just as fine, I think.

Changed January 22, 2009 12:51AM UTC by dz comment:4

What might be more useful is an exists that takes in a function -- something like:

$('.blah').exists(function(){ alert("I exist!"); }).something_else();

Changed January 22, 2009 07:27PM UTC by Thasmo comment:5

What about a merge of all this, kinda:

$.exists(param);

$('selector').exists(param);

Where param could be a css path,

a jquery object or a function,

and returns either the jquery object(s)

or FALSE.

Changed January 22, 2009 07:29PM UTC by Thasmo comment:6

Plus, 'param' is additional so it would

just check it the given objects exist,

like: $('#logo').exists();

Changed October 30, 2009 11:02PM UTC by mofle comment:7

This sounds great. Will it be in 1.4?

Changed November 19, 2010 02:26AM UTC by dmethvin comment:8

resolution: → worksforme
status: newclosed

This is the .is() method by another name.