Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raganwald/jquery-predicates
.exists() and does_not_exist() for jQuery
https://github.com/raganwald/jquery-predicates
Last synced: 7 days ago
JSON representation
.exists() and does_not_exist() for jQuery
- Host: GitHub
- URL: https://github.com/raganwald/jquery-predicates
- Owner: raganwald
- Created: 2010-09-14T04:27:23.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2012-10-15T14:12:11.000Z (about 12 years ago)
- Last Synced: 2023-04-12T08:06:54.271Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 103 KB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
jQuery Predicates
===[jQuery Predicates][pred] adds two new methods to jQuery:
* **exists** returns true if the selection is not empty. For example, `$('.preference-pane:visible').exists()` returns true if the preference pane is visible.
* **do\_not\_exist** returns true if the selection is empty. For example, `$('input.invalid').do_not_exist()` returns true if there are no `input` elements with class `invalid`.For your convenience, there are synonyms `.exist()` and `.does_not_exist()`, so your code can give a hint as to whether you expect there to be one or many elements. So you can write this:
outstanding_tasks = $('.task.outstanding')
if (outstanding_tasks.exist())
update_to_do_list(outstanding_tasks);
Or perhaps this:while ($('.notification-banner').does_not_exist()) {
// ...
}See for yourself:
;(function (jq_fn) {
jq_fn.exists = function () {
return !!(this.length);
};
jq_fn.exist = jq_fn.exists;
jq_fn.do_not_exist = function () {
return !(this.length);
};
jq_fn.does_not_exist = jq_fn.do_not_exist;
})(jQuery.fn);
If you don't want to use jQuery Predicates, by all means continue testing for `$('.foo:visible').length` or `$('input.invalid').length == 0`. But wouldn't you agree that it's more elegant to have code that says exactly what it means?p.s. jQuery Predicates plays well with [jQuery Combinators][comb]'s `.ergo(...)` and `.when(..)` methods.
p.p.s I'm writing a book called [CoffeeScript Ristretto](http://leanpub.com/coffeescript-ristretto). Check it out!
[comb]: http://github.com/raganwald/jQuery-Combinators
[pred]: http://github.com/raganwald/jQuery-Predicates