blob: a9075fcf06618f9fd6ef9cabdadf530201ef547b [file] [log] [blame]
Jon West6d00f9f2019-10-25 22:33:49 -04001/* * * * * * * * * * * * * * * * * *
2* Show/hide fields conditionally
3* * * * * * * * * * * * * * * * * */
4(function($) {
5 $.fn.conditionize = function(options){
6
7 var settings = $.extend({
8 hideJS: true
9 }, options );
10
11 $.fn.showOrHide = function(listenTo, listenFor, $section) {
12 if ($(listenTo).is('select, input[type=text]') && $(listenTo).val() == listenFor ) {
13 $section.slideDown();
14 }
15 else if ($(listenTo + ":checked").val() == listenFor) {
16 $section.slideDown();
17 }
18 else {
19 $section.slideUp();
20 }
21 }
22
23 return this.each( function() {
24 var listenTo = "[name=" + $(this).data('cond-option') + "]";
25 var listenFor = $(this).data('cond-value');
26 var $section = $(this);
27
28 //Set up event listener
29 $(listenTo).on('change', function() {
30 $.fn.showOrHide(listenTo, listenFor, $section);
31 });
32 //If setting was chosen, hide everything first...
33 if (settings.hideJS) {
34 $(this).hide();
35 }
36 //Show based on current value on page load
37 $.fn.showOrHide(listenTo, listenFor, $section);
38 });
39 }
40}(jQuery));
41
42 $('.conditional').conditionize();