Tiny HTMLSelectElement#selectedOptions shim for IE
npm install shim-selected-optionsTiny HTMLSelectElement#selectedOptions shim for IE.
* Can I use selectedOptions?
* WHATWG: HTML spec
* More conformant shim with IE8 support
Get the package from npm:
``sh`
npm install shim-selected-options --save
And simply require the module:
`js`
import "shim-selected-options"
Otherwise, plug it the old-fashioned way:
`html`
While making spec-perfect shim is technically possible,
it requires observing DOM subtrees to implement quirks of HTMLCollection,
not to mention emulating live collection. For most cases, this solution is fine.
* New object instead of always the same
* NodeList instead of HTMLCollection instance
* Static collection instead of live
See more on collections in WHATWG DOM spec.
Straightforward for single-value selects:
`diff`
- let {text} = select.options[select.selectedIndex]
+ let [{text}] = select.selectedOptions
Precise typing for multiple selects:
`diff``
- let opts = select.querySelectorAll(":checked") // NodeList
+ let opts = select.selectedOptions // HTMLCollection