Set value for Select – JavaScript

This JavaScript function allows you to set the value of a select tag without having to know its position in the list.

function setValueSelect(SelectName, Value) {
     SelectObject = document.getElementById(SelectName);
     for(index = 0;  index < SelectObject.length;  index++) {
            if(SelectObject[index].value.toLowerCase() == Value.toLowerCase()) SelectObject.selectedIndex = index;
      }
}

Usage Example:

<select id="clang">
<option value="arabic">Arabic</option>
<option value="english">English</option>
<option value="french">French</option>
<option value="persian">Persian</option>
<option value="turkish">Turkish</option>
</select>
setValueSelect("clang", "persian");

This selects the persian value in the select.

Related Posts Related Websites
Help sharing and Flatter me ;)

Leave a Reply

Follow me