This time I will share jQuery Plugin and tutorial about Conditionally Toggle Form Controls Based On Values – Conditionize, hope it will help you in programming stack.
Conditionize is a jQuery plugin that toggles the visibility of form controls based on the value(s) of a form field (input, select, checkbox, etc).
How to use it:
1. Download and place the JavaScript file conditionize.min.js
after jQuery library.
2 |
integrity = "sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" |
3 |
crossorigin = "anonymous" ></ script > |
4 |
< script src = "dist/conditionize.min.js" ></ script > |
2. Apply the condition logic to form controls using the data-cond
attribute as follows:
01 |
< form class = "example" action = "#" > |
03 |
< input type = "text" name = "text-control" placeholder = "Type 'jQueryScript'" > |
05 |
< div data-cond = "[name=text-control] == jQueryScript" >Text To Show Once You Typed 'jQueryScript'.</ div > |
07 |
< select name = "select-control" > |
08 |
< option value = "1" >One</ option > |
09 |
< option value = "2" >Two</ option > |
10 |
< option value = "3" >Three. Wow, you will see the new control below...</ option > |
13 |
< div data-cond = "[name=select-control] == 3" > |
15 |
< input type = "checkbox" name = "checkbox-control" > |
18 |
< span data-cond = "[name=checkbox-control] != true" >Nope</ span > |
19 |
< span data-cond = "[name=checkbox-control]" >Yep</ span > |
24 |
< div data-cond = "[name=select-control] == 3 && [name=checkbox-control] == true" > |
3. Hide the conditional form fields on page load.
4. Call the function on the form
and done.
3 |
$( '.example' ).conditionize(); |
5. Customize the default selector & attribute:
1 |
$( '.example' ).conditionize({ |
2 |
selector: '[data-cond]' , |
3 |
conditionAttr: 'data-cond' |
6. Specify how often to check the condition logic.
1 |
$( '.example' ).conditionize({ |
7. Customize the toggle function.
1 |
$( '.example' ).conditionize({ |
8. Event handlers.
01 |
$( '.example' ).conditionize({ |
Changelog:
v1.0.5 (2020-05-30)
v1.0.4 (2020-01-21)
v1.0.3 (2019-11-22)
v1.0.2 (2019-11-04)
- removed rafl usage (causes bugs in Visual Portfolio when no tab active)
v1.0.1 (2018-11-13)
- fixed usage of global $ variable, use jQuery instead
This awesome jQuery plugin is developed by nk-o. For more Advanced Usages, please check the demo page or visit the official website.