Picking selected options in a multiple select

user3600161

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.

Markus

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?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to determine which options selected in multiple <select>?

From Dev

Assigning 'selected' to options of multiple select element

From Dev

clear checkboxes of selected options in jquery multiple select

From Dev

Getting Multiple Select Box Options to Stay Selected Using Chosen Select

From Dev

How to find the sum of multiple select's selected options?

From Dev

Length for selected options of a select multiple always return 1

From Dev

get all the options selected of a multiple select without jQuery

From Dev

How to display all selected options using multiple bootstrap-select?

From Dev

Select2 multiple initial pre-selected options

From Dev

Sorting a select element with multiple options selected reverses the order

From Dev

get all the options selected of a multiple select without jQuery

From Dev

Require input field to be filled only if one of multiple select options selected

From Dev

Get selected option in Select2 event, when multiple options can be selected

From Dev

Options for Select, stick at selected Value

From Dev

Get the name of all selected items from select multiple="multiple" options dropdown

From Dev

select multiple options at a time

From Dev

Angular selected for multiple select

From Dev

Disabling non 'selected' options in multiple select elements except for one using jquery

From Dev

How can one set selected options for a cake php FormHelper multiple select?

From Dev

Angular ngRepeat multiple select fields without displaying already selected data through ng-options

From Dev

How to set an option from multiple options or array with different values to views as selected in select box using PHP

From Dev

options_for_select selected value not working

From Dev

Retrieve all selected options from a multi select

From Dev

Rails options_for_select with selected model value

From Dev

Change options in same select based on selected option

From Dev

Change select options based on another selected option

From Dev

AngularJs and <select>: Default selected options are removed

From Dev

Retrieve all selected options from a multi select

From Dev

Add selected options to Select2

Related Related

  1. 1

    How to determine which options selected in multiple <select>?

  2. 2

    Assigning 'selected' to options of multiple select element

  3. 3

    clear checkboxes of selected options in jquery multiple select

  4. 4

    Getting Multiple Select Box Options to Stay Selected Using Chosen Select

  5. 5

    How to find the sum of multiple select's selected options?

  6. 6

    Length for selected options of a select multiple always return 1

  7. 7

    get all the options selected of a multiple select without jQuery

  8. 8

    How to display all selected options using multiple bootstrap-select?

  9. 9

    Select2 multiple initial pre-selected options

  10. 10

    Sorting a select element with multiple options selected reverses the order

  11. 11

    get all the options selected of a multiple select without jQuery

  12. 12

    Require input field to be filled only if one of multiple select options selected

  13. 13

    Get selected option in Select2 event, when multiple options can be selected

  14. 14

    Options for Select, stick at selected Value

  15. 15

    Get the name of all selected items from select multiple="multiple" options dropdown

  16. 16

    select multiple options at a time

  17. 17

    Angular selected for multiple select

  18. 18

    Disabling non 'selected' options in multiple select elements except for one using jquery

  19. 19

    How can one set selected options for a cake php FormHelper multiple select?

  20. 20

    Angular ngRepeat multiple select fields without displaying already selected data through ng-options

  21. 21

    How to set an option from multiple options or array with different values to views as selected in select box using PHP

  22. 22

    options_for_select selected value not working

  23. 23

    Retrieve all selected options from a multi select

  24. 24

    Rails options_for_select with selected model value

  25. 25

    Change options in same select based on selected option

  26. 26

    Change select options based on another selected option

  27. 27

    AngularJs and <select>: Default selected options are removed

  28. 28

    Retrieve all selected options from a multi select

  29. 29

    Add selected options to Select2

HotTag

Archive