Select <select> tag options using value

I want to select an option in the select tag via value. - javascript

var selectbox=document.getElementById("Lstrtemplate");
var TemplateName=selectbox.options[selectbox.selectedIndex].text;

Now I have the option text in TemplateName, using this, I want to update another select tag that has the same text.

But I do not want to use an index or identifier.

You want to reach only the value

Please help me

0
source share
3 answers

Try the following:

var TemplateName = selectbox.options[selectbox.selectedIndex].value;
+1
source

I am not sure if I am interpreting your question correctly. You have two choices, and you want the changes in one reflected in the other. I assume that they both have the same set of parameters, and you need to synchronize them.

If yes, then:

var selectbox=document.getElementById("temp1");
var selectbox2=document.getElementById("temp2");
selectbox2.value = selectbox.value;

temp2 temp1, temp1 temp2 - select.

+1

The example above (selectobx2.value = selectbox.value) will correlate 2 selection elements based on value, from your description, I think that you want to adjust 2 selection elements based on display value or text property.

Unfortunately, there is no shortcut for this, and you need to list the parameters yourself, looking for the parameter whose text property matches the one you are looking for.

var selectbox=document.getElementById("Lstrtemplate");
var TemplateName=selectbox.options[selectbox.selectedIndex].text;

var select2 = document.getElementById("SecondSelect");
for (optionElem in select2.options)
    if (optionElem.text == TemplateName)
    {
        select2.value = optionElem.value;
        break;
    }
0
source

Source: https://habr.com/ru/post/1796695/


All Articles