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.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • FriendFeed
  • Meneame
  • Netvibes
  • Reddit
  • StumbleUpon
  • Tumblr
  • Wikio
  • RSS
  • email
  • PDF
  • Print

Related posts:

  1. jsSelect – Multiple select value modifier
  2. JavaScript to return dimensions
  3. jsCheckBox – Advanced select unselect checbox
  4. Input select generated from query – PHP
  5. Open Classifieds 1.5.3 Released

Leave a Reply

Follow me