Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and select box

Status
Not open for further replies.

project3

Technical User
Jan 10, 2008
22
US
I am trying to figure this out now for a bit and its driving me nuts.

I have values in a database that are for a select box they are not in a certain order so .listindex is out of the order. I know the value of the select box and send that to my function. how can i change the selected on without listindex.

like i know the right value is abc

<option SELECTED value="abc">abc</select>
<option value="def">def</select>

see what i want to do is be able to change the selected from one to the other with a function without using listindex as i know how to do that already. and i can't use a if statement on values like abc as they could change if the database values change.
 
Code:
function setSelectToValue(objSelect, strValue){
 for(var i = 0; i < objSelect.options.length; i++){
  if(objSelect.options[i].value == strValue){
   objSelect.options[i].selected = true;
   break;
  }
 }
}

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top