-
-
Save guihatano/587f812c8797f9421ff2ef501b7fd226 to your computer and use it in GitHub Desktop.
Show/Hide form fields based on the value in another field.
This file contains 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 characters
(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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment