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!

How do I set selectedIndex 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
Hi, I need to set the selectedIndex of a select element based on users input in a text box.

I was hoping I read enough examples to do it myself but I guess I was dreaming.

The following code does not generate any errors, but, the selected index does not change either. Any help appreciated.
Code:
var strUserEntered=document.getElementById('LicenseType').value.trim();
sel=document.getElementById('LicenseTypeOther');
var elementArrayLength = sel.length;
for (i=0;i<elementArrayLength;i++) {
  if (sel[i].value==strUserEntered) {
    sel.selectedIndex = i;
  }
}

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Lyndon, you were pretty close. Try this change:
Code:
var strUserEntered=document.getElementById('LicenseType').value.trim();
sel=document.getElementById('LicenseTypeOther');
var elementArrayLength = sel.length;
for (i=0;i<elementArrayLength;i++) {
  if (sel[!].options[i][/!].value==strUserEntered) {
    sel.selectedIndex = i;
  }
}

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
That did it!

Thanks kaht

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top