Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Last active February 28, 2018 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chukShirley/dcf835468292012d9ca1671c4259d016 to your computer and use it in GitHub Desktop.
Save chukShirley/dcf835468292012d9ca1671c4259d016 to your computer and use it in GitHub Desktop.
Retrieve data-* attributes from local data for option elements
<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>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment