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

text from combo box

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
I have something like the following:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<HTTP>
<HEAD>
</HEAD>
<BODY>
<FORM name="f1" id="f1">
<SELECT name="field1" id="field1" onchange='L1()'>
<option value="C">Corn
<option value="W">Wheat
<option value="S" selected>Soybeans
<option value="R">Rye
</SELECT>
<INPUT name="field2" id="field2"></INPUT>
</FORM>
<script language='VBScript'>
<!--
Sub L1()
f1.field2.value = f1.field1.value
End Sub
-->
</script>
</BODY>
</HTTP>
When a selection is made, this loads the value ("W" for Wheat) into the field2. I need it to load the text (Wheat) into the second field.
(In the live application, the combo box is loaded from a recordset.)
Is there any way to reference the text value of the combo box?
 
Maybe you could use the "label=" attribute and get at it through the DOM that way.

Code:
<SELECT name="field1" id="field1" onchange='L1()'>
  <option value="C" label="Corn">Corn</option>
  <option value="W" label="Wheat">Wheat</option>
  <option value="S" label="Soybeans" selected>Soybeans</option>
  <option value="R" label="Rye">Rye</option>
</SELECT>
 
Thank you.
Using in the SUB

f1.field2.value = f1.field1.label

I get an error message

Object doesn't support this property or method: 'f1.field1.label'
 
Get the dropdown's selected value by first getting the selected index
f1.field2.value = document.getElementById(field1).options[document.getElementById(field1).options.selectedIndex].value;

 
you can just set the value to wheat, corn, soybean, rye and then get the first letter of that value for what you want.
e.g.
Code:
<SELECT name="field1" id="field1" onchange='L1()'>
  <option value="Corn">Corn</option>
  <option value="Wheat">Wheat</option>
  <option value="Soybeans" selected>Soybeans</option>
  <option value="Rye">Rye</option>
</SELECT>
<INPUT name="field2" id="field2"></INPUT>
</FORM>
<script language='VBScript'>
<!--
Sub L1()
  f1.field2.value = f1.field1.value
  other_field = left(f1.field1.value, 1)
End Sub
-->
</script>
 
[1]
>Is there any way to reference the text value of the combo box?
> f1.field2.value = f1.field1.value
Am I missing something with this thread? To reference the text value, you can do it like this.
[tt] f1.field2.value = f1.field1(f1.field1.selectedIndex).text[/tt]

[2] Also, what is this [tt][red]<HTTP></HTTP>[/red][/tt]? Never heard of it! It is [tt][blue]<html></html>[/blue][/tt].
 
I just noticed you are using VBScript instead of Javascript. Isn't that only going to work in browsers that can execute VBScript client-side? That is, IE only? Anyway, the code I posted earlier is the Javascript version of what you need.
 
>Anyway, the code I posted earlier is the Javascript version of what you need.
>f1.field2.value = document.getElementById(field1).options[document.getElementById(field1).options.selectedIndex].value;

Only if you get all the ingredients right, it still needs precision rather than just approximative.

[tt]f1.field2.value = document.getElementById([red]"[/red]field1[red]"[/red]).options[document.getElementById([red]"[/red]field1[red]"[/red]).options.selectedIndex].[red]text[/red];[/tt]

But then, that is only apparently javascript nominally for "all common" browsers; in fact that is still targetted for ie which, by the way, nothing wrong just like using client-side vbscript that people seem particularly like kind of knee-jerk comment on it. This is the cross-browser effort.
[tt]
[blue]document.getElementById("f1").elements["field2"][/blue].value = document.getElementById([red]"[/red]field1[red]"[/red]).options[document.getElementById([red]"[/red]field1[red]"[/red]).options.selectedIndex].[red]text[/red];
[/tt]
 
<off-topic>
It seems when Sheco's away, gangco will play.
</off-topic>
 
Um, yeah, very sloppy of me. I copied the code in haste from something I wrote that used a variable for the field and was returning the selected value instead of the text.

On quotes: I've noticed that IE7 doesn't like double quotes in the getElementById("field1"). Not sure if this is so in IE6, nor if it is so in all situations. getElementById('field1') has worked better for me.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top