Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1643 How can I extract all the selected options from a multiple selection list?

You are here: irt.org | FAQ | JavaScript | Form | Q1643 [ previous next ]

Try something like:

<form>
<select name="mySelect" multiple size="3">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
<option>7
<option>8
<option>9
</select>
<input type="button" value="Get Selected" onClick="getSelected(this.form.mySelect)">
</form>

<script language="JavaScript"><!--
var selects = new Array();

function getSelected(select) {
    for (var i=0, j=0; i<select.options.length; i++) {
        if (select.options[i].selected) {
             selects[j++] = select.options[i].text;
        }
    }

    var output = '';
    for (var i=0; i<selects.length; i++) {
        output += selects[i] + '\n';
    }
    alert(output);
}
//--></script>

©2018 Martin Webb