I'm surprised I can't find this code online!
How do I access ALL the selected indices of a select list? Not just the first one?
HTML:
<select name="trends[]" multiple="multiple" id="trends" size="35"></select>
js:
function moveSelectedTrends()
{
var selectedTrends = document.getElementById('trends');
for(var i=0; i < selectedTrends.length; i++)
{
alert(selectedTrends.options[selectedTrends.selectedIndex].value) //ONLY returns the first selected element!
}
}
Use i
instead of selectedTrends.selectedIndex
and test if it is selected
.
function moveSelectedTrends() {
var trends = document.getElementById('trends'), trend, i;
for(i = 0; i < trends.length; i++) {
trend = trends[i];
if (trend.selected) {
alert(trend.value);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With