user3600161
user3600161

Reputation: 49

Picking selected options in a multiple select

I have a multiple DDL with items built dynamically with PHP and I want to store the selected options on page load, so as to be able to restore them later discarding the changes made.

So, after building the DDL:

var store = [];
ob = document.getElementById('getall_writers'); nr = ob.length;
for(var i=0; i < nr; i++) {
    if(ob.options[i].selected) { store[i] = i ;}
}

What is my mistake? It always has store.length=2.

Upvotes: 0

Views: 48

Answers (1)

Markus
Markus

Reputation: 5797

I just tested and modified it a little bit to store the value (not the id), but it looks good: http://jsfiddle.net/ts92oafp/1/

var store = [];
var ob = document.getElementById('getall_writers');
var nr = ob.length;
for(var i=0; i < nr; i++) {
    var option = ob.options[i];
    if(option.selected) {
        store.push(option.value);
    }
}
console.log(store);

Isn't this what you want?

Upvotes: 1

Related Questions