Created
May 22, 2014 00:43
-
-
Save masonjm/1c221475fc099c90ba30 to your computer and use it in GitHub Desktop.
Revisions
-
masonjm created this gist
May 22, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ (function( $ ){ $.fn.dependsOn = function(element, value) { var elements = this; var hideOrShow = function() { var $this = $(this); var showEm; if ( $this.is('input[type="checkbox"]') ) { showEm = $this.is(':checked'); } else if ($this.is('select')) { var fieldValue = $this.find('option:selected').val(); if (typeof(value) == 'undefined') { showEm = fieldValue && $.trim(fieldValue) != ''; } else if ($.isArray(value)) { showEm = $.inArray(fieldValue, value.map(function(v) {return v.toString()})) >= 0; } else { showEm = value.toString() == fieldValue; } } elements.toggle(showEm); } //add change handler to element $(element).change(hideOrShow); //hide the dependent fields $(element).each(hideOrShow); return elements; }; $(document).on('ready page:load', function() { $('*[data-depends-on]').each(function() { var $this = $(this); var master = $this.data('dependsOn').toString(); var value = $this.data('dependsOnValue'); if (typeof(value) != 'undefined') { $this.dependsOn(master, value); } else { $this.dependsOn(master); } }); }); })( jQuery );