Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
18 lines (12 sloc) 1 KB
layout title
post
Dropdown change events and selectedIndex

Today I learned how to listen to <select> change events, and store the selected option in plain-jane javascript:

See the Pen phLAj by Jesse Shawl (@jshawl) on CodePen.

<script async src="//codepen.io/assets/embed/ei.js"></script>

I had never heard of the selectedIndex method, so I checked out the documentation on the mozilla developer network.

I'm not sure this is super useful outside the context of dropdown menus, but it's a heck of a lot easier than looking for selected attributes and removing them, which can be a pain to manage.

Instead, you can just set selectNode.selectedIndex to -1, which will deselect all options.

You can’t perform that action at this time.