Data Filter filters elements by data attribute.

Get it

Download .zip Download .tar.gz View on GitHub

Usage

At LayerVault, we make heavy use of filtering DOM elements by their data-* attributes. That's because we believe JavaScript is the worst place to keep your Model.

We might have a dozen or so data-* attributes attached to different DOM elements. You'll be surprised how much easier your life will be.

Old Way

var versions = $('.Revision')
  .filter('[data-project-id="' + projectId + '"]');

New Way with jquery.data.filter.js

var versions = $('.Revision').dfilter('project-id', projectId);

You can even opt to only check against existence of a non-blank data-* attribute:

var versions = $('.Revision').dfilter('project-id');
var sameVersions = $('.Revision').dfilter('project-id', true);

Or, give it an array!

var versions = $('.Revision').dfilter('project-id', [100, 101]);

You can also filter using true and false. A value of true filters elements with that data attribute, even if the attribute is blank. A value of false will select all things without that data attribute:

// Selects all revisions without a project-id
var versions = $('.Revision').dfilter('project-id', false);

License

Copyright 2013 LayerVault Inc.

Licensed under the MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.