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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change selected value of drop down 1

Status
Not open for further replies.

mwa

Programmer
Jul 12, 2002
507
US
Hey Evertbody...

I have a function that gets passed several values. One of these values is related to an option value on a <select>. Based on the passed value, I would like to set the &quot;selected&quot; option of the <select> box. Is there a way to do this using javascript? I read thread216-556071 which tells you how to do this using the selectedindex, but I would like to do this using the value instead. Is this even possible?

Thanks in advance,
mwa
 
you have to loop thru the select box options checking the value against the passed in value, when the 2 match set the selected property to true

for (var i=0;i<document.FormName.SelectName.options.length;i++) {
if (document.FormName.SelectName.options.value==PassedInValue) {document.FormName.SelectName.options.selected=true}
}
 
Thanks sjravee.... But, I couldn't quite get yours to work as is... I was able to modify it a little and get it to work... Here is what I came up with:

function MyChangeSelect(val)
{
for (i=0;i<document.myForm.mySelect.options.length;i++)
{
document.myForm.mySelect.selectedIndex = i
if (document.myForm.mySelect.value == val)
{return true}
}
}
Thanks again for your help... TGIF...

Mitch
 
sorry, that if statement was meant to say

if (document.FormName.SelectName.options.value==PassedInValue) {document.FormName.SelectName.options.selected=true}
}
 
it doesnt look like i can write square brackets in these posts, they make seem to make the post italic but where ever you see 'SelectName.options' in the if satement put i in square brackets immediately after the options word.

In your method you are setting the selectedindex property of the select, if you had a lot of options the user would see some funnny business happening in the select box, thats why you use the counter to loop thru the select as this does not cause the select box to change its current value.
 
Ahhh... I see... I just added the [] and the i and it works now....

Thanks for your help,
mwa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top