Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/biohzrdmx/jquery.conditional

Show and hide things depending on the value of other things.
https://github.com/biohzrdmx/jquery.conditional

Last synced: about 2 months ago
JSON representation

Show and hide things depending on the value of other things.

Awesome Lists containing this project

README

        

jQuery Conditional
==================

Show and hide things depending on the value of other things.

Now with 100% more HTML5!

### Basic usage ###

Basically, you'll have a `select` element that will have some options for the user to choose from.

Then, you'll have some other controls that you may want to show depending on the selected option from the `select` box.

So you'll have to add a `data-conditional` attribute to your `select`, give it a meaningful value, say 'foo'.

Now you may want to wrap the conditionally-shown elements in groups, so you can give each group a `data-condition` attribute with the name of the condition (in this case 'foo') and a `data-match` one with the actual value you want to match from the `select`.

Check the demo below and its source to see how it works.

### Demo source ###


Choose a book


Birds from 'Murica
They see me rollin'




Your favorite bird was*



Your least-favorite bird was*





Why do you think they're hatin'?*


As you can see, its pretty straightforward:

1. Add the `data-conditional` attribute to your `select`
1. Wrap your conditional elements, add `data-condition` and `data-match` to the wrapper.
1. Profit!

### Customization ###

You may want to change the CSS class or the event, heck, you may even want to provide your own toggle logic.

That's completely possible if you override the `defaults` object inside `$.conditional`

Here's what it looks like:

$.conditional = {
defaults: {
className: 'hide',
eventName: 'change',
onActivate: function(element, opts) {
element.removeClass(opts.className);
},
onDeactivate: function(elements, opts, callback) {
elements.addClass(opts.className);
callback.call();
},
autoBind: true
}
};

### Troubleshooting ###

As usual, this should work on any _modern_ browser, that means you may run into issues with the crappy, glue-eating ones.

If you have an idea/fix, please feel free to fork the repo, add your fix/feature and send me a push request.

### License ###

Copyright © 2015 biohzrdmx

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.