Click to See Complete Forum and Search --> : Changing selected option in <SELECT>


TBor
05-28-2003, 10:25 AM
Hi all. Hopefully this is an easy one:

I'm trying to have a VBScript subroutine change the selected value of a SELECT box. However, the SELECT box is dynamically filled from database values (ASP), so selecting the Option statement that I want by specifying an integer for selectedIndex is pretty much out of the question.

I would like to change the Option selected based on the Value of that Option, if possible. I know the Value is always going to be unique within the SELECT box (it's a user ID).

Here's an example of what I am looking for (of course, the subroutine as written doesn't work):

sub ChangeOption(optionValue)
document.form.name.selected.value = optionValue
end sub
...
<form>
<select id="name" name="name">
<option value="user1" selected>Dave</option>
<option value="user2">Bob</option>
<option value="user3">Joe</option>
<option value="user4">Steve</option>
</select>

<input type="button" onClick="ChangeOption 'user3'">


Seeing as the "real" SELECT list I'm using is ASP generated, and users change sometimes, I don't think I can use selectedIndex. For instance, as shown above, Joe would be selectedIndex=3 (or 2, I can't remember), but if Bob were to quit and be removed from the DB, Joe is still "user3", but the selectedIndex changes for Joe the next time the SELECT box options are generated.

Hopefully someone out here will let me know what to do, or if it can be done at all.

Thanks in advance,
TBor

TBor
05-29-2003, 08:29 AM
Still having trouble, but don't know why. Here's a chunk of the code I'm playing with:

sub ImproveEditClicked(assignedtoID)
set sel = document.frmPHRImprove.AssignedTo
sel.options(sel.selectedIndex).value=assignedtoID
msgbox document.frmPHRImprove.AssignedTo.value
end sub

<form name=frmPHRImprove id=frmPHRImprove>
...
<select id="AssignedTo" name="AssignedTo">
<option selected></option>
<option value="CA001234">Dave</option>
<option value="CA002345">Joe</option>
<option value="CA003456">Steve</option>
</select>
...
<input type="radio" id="edit" name="edit" value="9" onClick="ImproveEditClicked 'CA002345'">
...
</form>

I know that the code you gave me works, as the msgbox comes back wtih CA002345 displayed. However, "Joe" is not displayed in the SELECT box on the screen. Is there another piece to the puzzle that I'm missing?

Thanks,
TBor

DaiWelsh
05-30-2003, 05:08 AM
sel.options(sel.selectedIndex).selected = True

I may beout of my depth here and Dave Clark is a God, I am not worthy etc. but would this not set the currently selected item as selected (redundant?), TBor wants to set an item as selected but does not know which one it is. I believe the answer is to loop through the options looking for the one with the right value e.g. (and someone will need to do VBScript syntax correctly as I generally use javascript)

myValue="Joe"
for i = 0 to sel.options.length
if(sel.options[i].value = myValue) then
sel.options[i].selected = true
break
end if
next

TBor
05-30-2003, 07:50 AM
HALLELUJAH!!!

Thanks Dave & DaiWelsh for your help, even with the confusion about what I was trying to do. I've got to get better at using the correct terminology when asking for help. But, you figured out what I was trying to do regardless, and I appreciate that.

Everything works like a charm now!

Thanks,
TBor