Answers for "force select of an option index javascript"

0

force select of an option index javascript

//associate documentElements to var
let selectValue = document.getElementById('select1ID');
let selectKey = document.getElementById('select2ID');

//get all option values into given select 
function getSelectElem(selectOptions) {
    let arrayOptions = [];
    let arreyDim = selectOptions.length;
    for (let i = 0; i < arreyDim; i++) {
        arrayOptions[i] = selectOptions[i];
    }
    console.log(arrayOptions);
    return arrayOptions;
}

//get index of a specific value of a select 
function associaElemIndex(select, selectVal) {
    let indexValue;
    let valoriDisponibili = getSelectElem(select.options);
    valoriDisponibili.forEach((item, index) => {
        if (item.value === selectVal) {
            indexValue = index;
        }

    });
    return indexValue;

}

/*get value of the selected element in select1,
get index of the selected value
get elements of selct2
associate select option selected with index of select1


result: when select 1 get changed, select2 dinamically change to 
same index value to match select1 index
*/
select1ID.addEventListener('change', (e) => {
    let valoreAttuale = select1ID.value;
    let index = associaElemIndex(selectKey, valoreAttuale);
    let values = getSelectElem(select2ID);
    select2ID.options[index].selected = values[index];
});
Posted by: Guest on July-01-2021

Code answers related to "force select of an option index javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language