Categories
JavaScript

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:


setValueSelect("clang", "persian");

This selects the persian value in the select.