Created
September 21, 2016 13:40
-
-
Save mohamedrez/80ce75bb501159514fab29e3ecc9b417 to your computer and use it in GitHub Desktop.
jQuery: dependent filter
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
$('[data-depends-on]').each(function(){ | |
var $this = $(this); | |
var depends_on = $this.data('depends-on'); | |
$this.hide(); | |
$('[for="'+ $this.attr('id') + '"]').hide(); | |
$('#'+depends_on).on('change',function(event) { | |
var $this = $(this); | |
if($this.val()){ | |
var $depends_on_element = $('[data-depends-on="'+ depends_on + '"]'); | |
$depends_on_element.fadeIn(); | |
$('[for="'+ $depends_on_element.attr('id') + '"]').fadeIn(); | |
}else{ | |
$('[data-depends-on="'+ depends_on + '"]').fadeOut(); | |
$('[for="'+ depends_on + '"]').fadeOut(); | |
$('[data-depends-on="'+ depends_on + '"]').val(''); | |
$('[data-depends-on="'+ depends_on + '"]').trigger('change'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment