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

Selected option in HTA 1

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
In HTML, a dropdown box would be coded something like:
<html>
<body>
<select>
<option>Volvo</option>
<option selected="selected">Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
</body>
</html>
I have an HTA which needs to populate a dropdown box from a recordset. On load, the following code is run:
Do While Not rs.EOF
Set objOpt = Document.createElement("OPTION")
objOpt.Text = rs.fields(0)
objOpt.Value = rs.fields(0)
f1.selASC.Add(objOpt)
rs.MoveNext
Loop
This works very nicely except I need to have one of the options behave like the
<option selected="selected">Saab</option>
line in the first sample. I have been trying to use an IF statement to add the selected attribute when the value equals another field on the form. The IF statement works nicely, but nothing I've tried gets the
<option selected="selected">Saab</option>
correct.

How does this need to be coded?
 
What about this ?
objOpt.setAttribute "selected"
objOpt.setAttribute "defaultselected"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is how it can be done.
[tt]
objOpt.selected=true
'or
objOpt.setAttribute "selected",true
[/tt]
But there is a considerable implementation freedom in setting at its introduction to spec.
[tt]
objOpt.selected="anynonemptystring"
'or
objOpt.setAttribute "selected","anynonemptystring"
[/tt]
But more consistent meaningful value works for the scripter rather than against.
 
Thank you. I ended up using this:

objOpt.Value = rs.fields(0)
IF rs.fields(0) = "XBA" THEN
objOpt.selected=true
END IF
f1.selASC.Add(objOpt)
 
Well, this is rather interresting.

While trying to retrieve the value to which I wanted to set the dropdown, I ran

f1.selASC.value = cellval

Do While Not rs.EOF
Set objOpt = Document.createElement("OPTION")
objOpt.Text = rs.fields(0)
objOpt.Value = rs.fields(0)
f1.selASC.Add(objOpt)
rs.MoveNext
Loop

and it worked.

 
For select-one element, .value, .selectedIndex and .option(i).selected are maintained internally and you can set one to automatically get a consistent values for the others. But for select-multiple, that would stop short and consist of more independent settings; and posting data (not happening here) to server would behave differently.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top