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: |
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);
},
Change History (8)
comment:1 Changed 14 years ago by
comment:2 Changed 14 years ago by
Component: | unfilled → core |
---|---|
Milestone: | 1.3 → 1.3.1 |
Type: | enhancement → feature |
Version: | 1.2.6 → 1.3 |
comment:3 Changed 14 years ago by
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
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
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
Plus, 'param' is additional so it would just check it the given objects exist, like: $('#logo').exists();
comment:8 Changed 12 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
This is the .is() method by another name.
That sounds good - nice idea!