chuk-shirley
2/28/2018 - 8:49 PM

Use data-* attributes (dataset) with local data

Retrieve data-* attributes from local data for option elements

  let selectedVendorNumber = $('#mySelect2').select2('data')[0].element.dataset.vendorNumber;
  // N.b. The name of a custom data attribute in Javascript is the name of the same HTML attribute but in camelCase and with no dashes, dots, etc.
  // Using HTMLElement.dataset: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
<select id="mySelect2">
  <option value="1" data-vendor-number="5">1: Acme Industries</option>
  <option value="2" data-vendor-number="5">2: Your Company, LLC</option>
</select>