daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest May 19th, 2017 38 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.   // Split a state dropdown into <optgroup> tag based on country. Each optgroup contains the
  3.   // states/provinces for that country.  Currently Canada and the US.
  4.   (function() {
  5.     // Put 'US' before 'CN' to see states first.
  6.     var labels = { 'CN': 'Canada', 'US': 'United States' };
  7.     // Change "field-address-state" to "State" for Classic.
  8.     var stateSelector = 'select[name="field-address-state"]';
  9.     var provinces = 'BC,MB,NB,NL,SK,NT,NU,NF,NS,ON,PE,QC,YT'.split(',');
  10.     var stateField = document.querySelector(stateSelector);
  11.     var states = Array.from(stateField.querySelectorAll('option')).slice(1);
  12.     var optgroups = Object.keys(labels).reduce(function(a, n) {
  13.         a[n] = document.createElement('optgroup');
  14.         a[n].setAttribute('label', labels[n]);
  15.         stateField.appendChild(a[n]);
  16.         return a;
  17.     }, {});
  18.     var reducer = (a, e) => {
  19.         var key = (provinces.indexOf(e.value) != -1 ? 'CN' : 'US');
  20.         a[key].appendChild(e);
  21.         return a;
  22.     }
  23.     states.reduce(reducer, optgroups);
  24.    })();
  25. </script>
RAW Paste Data
Top