npm

shim-selected-options

1.0.1 • Public • Published

shim-selected-options

Tiny HTMLSelectElement#selectedOptions shim for IE.

Up and running

Get the package from npm:

npm install shim-selected-options --save

And simply require the module:

import "shim-selected-options"

Otherwise, plug it the old-fashioned way:

<script src="https://unpkg.com/shim-selected-options" async></script>

Caveats

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.

Examples

Straightforward for single-value selects:

- let {text} = select.options[select.selectedIndex]
+ let [{text}] = select.selectedOptions

Precise typing for multiple selects:

- let opts = select.querySelectorAll(":checked") // NodeList<Element>
+ let opts = select.selectedOptions // HTMLCollection<HTMLSelectElement>

Package Sidebar

Install

npm i shim-selected-options

Weekly Downloads

1,351

Version

1.0.1

License

MIT

Unpacked Size

2.45 kB

Total Files

3

Last publish

Collaborators

  • shvaikalesh