Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active April 2, 2019 18:16
Show Gist options
  • Select an option

  • Save aatronco/d56e4d317cee196fb72062420f693f15 to your computer and use it in GitHub Desktop.

Select an option

Save aatronco/d56e4d317cee196fb72062420f693f15 to your computer and use it in GitHub Desktop.
// This example will remove all Regions that are not "12" and all Municipalities not listed below.
var shipping_to = ["Cerrillos","Cerro Navia","Conchali","El Bosque","Estacion Central","Huechuraba","Independencia","La Cisterna","La Florida","La Granja","La Pintana","La Reina","Las Condes","Lo Barnechea","Lo Espejo","Lo Prado","Macul","Maipu","Ñuñoa","Padre Hurtado","Pedro Aguierre Cerda","PeÑAlolen","Providencia","Pudahuel","Puente Alto","Quilicura","Quinta Normal","Recoleta","Renca","San Bernardo","San Joaquin","San Miguel","San Ramon","Santiago Centro","Vitacura"]
function removeItems(select,shipping_to){
$('#' + select +' option:not(:first)').filter(function() {
return $.inArray($.trim($(this).text()), shipping_to) == -1;
}).remove();
};
$("#order_shipping_address_region").change(function(){
var interval = setInterval(function(){
if($("#order_shipping_address_municipality").text()){
removeItems("order_shipping_address_municipality",shipping_to);
clearInterval(interval)
}
}, 500)
});
$(document).ready(function(){
var interval = setInterval(function(){
$("#order_shipping_address_region").val("12");
if( $("#order_shipping_address_region").val() == "12"){
clearInterval(interval)
// fire native change event on select element -- START
element = document.getElementById('order_shipping_address_region')
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
element.dispatchEvent(evt);
// fire native change event on select element -- END
removeItems("order_shipping_address_region",["Metropolitana"]);
}
}, 500)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment