Last active
November 8, 2019 11:25
-
-
Save JohnKingsbury/6e103d3e9f2b865c431ccfbb4fcb8842 to your computer and use it in GitHub Desktop.
Quick IE compatible method to dynamically filter selections
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
$.fn.showOption = function() { | |
this.each(function() { | |
var span = $(this).parent(); | |
var opt = $(this).show(); | |
$(span).replaceWith(opt) | |
}); | |
} | |
$.fn.hideOption = function() { | |
this.each(function() { | |
if ($(this).parent()[0].tagName != "SPAN") { | |
$(this).wrap('<span>').hide(); | |
} | |
}); | |
} | |
function filterSelect(parentName, childName) | |
{ | |
$("#"+ parentName).change(execFilter); | |
execFilter(); | |
function execFilter() | |
{ | |
var parentValues = $("#" + parentName).val(); | |
if(parentValues === undefined){ | |
return; | |
} | |
var childField = $("#" + childName); | |
var optionValueSelector; | |
if(parentValues.map) | |
{ | |
optionValueSelector = parentValues.map(function(selectedValue) | |
{ | |
return "[value^='" + selectedValue + "-']"; | |
}).join(","); | |
} | |
else | |
{ | |
optionValueSelector = "[value^='" + parentValues + "-']"; | |
} | |
var childVal = childField.val(); | |
childField.find("option").hideOption(); | |
childField.find("option[value='']," + optionValueSelector).showOption(); | |
childField.val(childVal); | |
childField.val(childField.val()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment