Skip to content

Instantly share code, notes, and snippets.

@abombss
Created June 17, 2011 05:40
Show Gist options
  • Save abombss/1030929 to your computer and use it in GitHub Desktop.
Save abombss/1030929 to your computer and use it in GitHub Desktop.
JQuery plugin for Cascading Selects
(function ($) {
/// <summary>JQuery plugin for handling cascading html select lists</summary>
/// <param name="$" type="jQuery">JQuery</param>
$.fn.cascadingSelect = function() {
this.each(function() {
var $e = $(this);
var $p = $($e.attr("data-ui-cascading-parent"));
$e.data('cascading-opts', $e.find("option:gt(0)"));
$p.bind("change", function() {
$e.find("option:gt(0)").remove();
$e.append($e.data('cascading-opts').filter('[data-ui-cascading-parent=' + this.value + ']'));
if ( $e.children().size() == 2 ) {
$($e.children().get(1)).prop("selected", true);
}
$e.change();
});
$e.find("option:gt(0)").filter(":not(:selected)").remove();
});
return this;
};
})(jQuery);
// Usage
// $("select[data-ui-cascading-parent]").cascadingSelect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment