force select of an option index javascript

Solutions on MaxInterview for force select of an option index javascript by the best coders in the world

showing results for - "force select of an option index javascript"
Emily
03 Nov 2017
1//associate documentElements to var
2let selectValue = document.getElementById('select1ID');
3let selectKey = document.getElementById('select2ID');
4
5//get all option values into given select 
6function getSelectElem(selectOptions) {
7    let arrayOptions = [];
8    let arreyDim = selectOptions.length;
9    for (let i = 0; i < arreyDim; i++) {
10        arrayOptions[i] = selectOptions[i];
11    }
12    console.log(arrayOptions);
13    return arrayOptions;
14}
15
16//get index of a specific value of a select 
17function associaElemIndex(select, selectVal) {
18    let indexValue;
19    let valoriDisponibili = getSelectElem(select.options);
20    valoriDisponibili.forEach((item, index) => {
21        if (item.value === selectVal) {
22            indexValue = index;
23        }
24
25    });
26    return indexValue;
27
28}
29
30/*get value of the selected element in select1,
31get index of the selected value
32get elements of selct2
33associate select option selected with index of select1
34
35
36result: when select 1 get changed, select2 dinamically change to 
37same index value to match select1 index
38*/
39select1ID.addEventListener('change', (e) => {
40    let valoreAttuale = select1ID.value;
41    let index = associaElemIndex(selectKey, valoreAttuale);
42    let values = getSelectElem(select2ID);
43    select2ID.options[index].selected = values[index];
44});