Forum Moderators: open

Message Too Old, No Replies

Javascript Update Select using values

         

too much information

4:46 pm on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a javascript that updates a form select box by the option method, but how do I get the field to update based on the value of the option, not the order of its occurance?

Here's what I have so far:

document.editCompany.companyCategory.options["14"].selected = true;

What I'm looking for is something like this:

document.editCompany.companyCategory.options.value["blue widgets"].selected = true;

lavazza

6:42 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



I'd nest all of the content with a <form>
e.g.

<form name="frmMyFormName"
method="post"
action="http://www.WHATEVER_WHATEVER_WHATEVER">

and then use


document.frmMyFormName.mySelectName.selectedIndex=0;

or

document.frmMyFormName.mySelectName.selectedIndex=14;

(remembering that selects are zero based,
so '
selectedIndex=14
' sets the 15th option)

too much information

2:04 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's almost what I had already which works, but I didn't want to have to count the number of choices in the select box to determine what the choice whould be in the case that the database was updated and I had a new number of items in the select box.

Here's what I was able to find:

function Select_Value_Set(SelectName, Value)
{
eval('SelectObject = document.' + SelectName + ';');
for(index = 0; index < SelectObject.length; index++)
{
if(SelectObject[index].value == Value)
SelectObject.selectedIndex = index;
}
}

then just use:

Select_Value_Set("editCompany.companyCategory", "14");

to set the index. I wish I could credit the author but I've been through so many samples of code today I can't remember where this one came from.