Bug Tracker

Opened 14 years ago

Closed 12 years ago

#3842 closed feature (worksforme)

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:


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);


Change History (8)

comment:1 Changed 14 years ago by Thasmo

That sounds good - nice idea!

comment:2 Changed 14 years ago by john

Component: unfilledcore
Type: enhancementfeature

comment:3 Changed 14 years ago by balazs.endresz

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.

comment:4 Changed 14 years ago by dz

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

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

comment:5 Changed 14 years ago by Thasmo

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.

comment:6 Changed 14 years ago by Thasmo

Plus, 'param' is additional so it would just check it the given objects exist, like: $('#logo').exists();

comment:7 Changed 13 years ago by mofle

This sounds great. Will it be in 1.4?

comment:8 Changed 12 years ago by dmethvin

Resolution: worksforme
Status: newclosed

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

Note: See TracTickets for help on using tickets.