Last active
February 28, 2018 20:54
-
-
Save chukShirley/dcf835468292012d9ca1671c4259d016 to your computer and use it in GitHub Desktop.
Retrieve data-* attributes from local data for option elements
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
<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> |
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
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