Getting selected options with querySelectorAll

GOTO 0 picture GOTO 0 · Mar 23, 2013 · Viewed 33.7k times · Source

I wonder if it's possible in Javascript to get the currently selected options in a <select multiple> field using the Selctors API rather than a "stupid" iteration over all options.

select.querySelectorAll('option[selected="selected"]') only returns the options that were marked as preselected in the original HTML, which is not what I'm looking for. Any ideas?

Answer

a better oliver picture a better oliver · Mar 23, 2013

document.querySelectorAll('option:checked')

Works even on IE9 ;)