Skip to content

Instantly share code, notes, and snippets.

@guihatano
Forked from masonjm/depends_on.js
Created August 28, 2020 00:10
Show Gist options
  • Save guihatano/587f812c8797f9421ff2ef501b7fd226 to your computer and use it in GitHub Desktop.
Save guihatano/587f812c8797f9421ff2ef501b7fd226 to your computer and use it in GitHub Desktop.
Show/Hide form fields based on the value in another field.
(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