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

Can I grab the selected item in a drop down box (not the value)?

Status
Not open for further replies.

jkafer

Technical User
Feb 13, 2004
32
US
Ok - this sounds funny, but if there is a way I can do this it would make everything come together...

Here is my form:

<form name="form1" method="post" action="displaycolor.asp">
<select name="selColor" size="1">
<option value="1">Green</option>
<option value="2">Yellow</option>
<option value="3">Blue</option>
<option value="4">Red</option>
</select>
</form>

Now, the "Value" of each option gets passed to "displaycolor.asp". But, is there a way for me to get the item that was selected.

For example:
If I selected Green, the value 1 is passed to the action program, but is there any way I could pull "Green" with it?

The reason I ask is that that actual program I am doing this in has active dropdowns (5 or more), (code gotten from the Javascript Forum on this website - thank you very much), where dropdown2 is filled in after a selection in dropdown2 is selected, so on and so on with the next 5 fields. So I cannot do the "If value=txt then write "selected" when the dropdown are written on the next page. The values all come together to create a partnumber that is looked up in a database and displayed. We want to be able to show them the selections they made from the dropdowns when it is printed.
 
Why not change the value to the color?

Code:
<option value="green">Green</option>
    <option value="yellow">Yellow</option>
    <option value="blue">Blue</option>
    <option value="red">Red</option>


There are other ways, but this is the easiest


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

 
The values in the selection box are used to create a part number that is looked up in a database. Sort of like selecting options in your new car, it builds a model number that doesn't necessarily directly reflect the color of the seats that you have choosen.

Field 1
Selected "Green" Value = 1
There could be 25 colors in this drop down.
Depending on what is selected, will fill with what is available in Field 2, otherwise all colors would show.

Field 2
Selected "Bucket" Value = 2


Field 3
Selected "Leather" value = "L"


So I can build a partnumber with your selections, but I want to be able to show you (the user, in terms they will understand - simply) what they have selected. Rather than the technical description when the part number is looked up.
 


Code:
<form name="form1" method="post" action="displaycolor.asp">
  <select name="selColor" size="1" [blue]onChange="showColor()"[/blue]>
    <option value="1">Green</option>
    <option value="2">Yellow</option>
    <option value="3">Blue</option>
    <option value="4">Red</option>
  </select>
  [blue]<input type=hidden name=hide1>[/blue]
</form>
[blue]<script>
function showColor(){
  	selIndex = document.form1.selColor.selectedIndex
	selColor = document.form1.selColor.options[selIndex].text
	alert(selColor)
	//to pass it 
	[red]document.form1.hide1.value = selColor[/red]
}
</script>[/blue]

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

 
I should probably move this to the Javascript forum.

This will work great and I thank you greatly for it.

The only thing I need help with is making this code a little more generic. Meaning: I have 5 fields on this form, each already has a OnChange action, and from reading posts, I can run 2 javascripts through the OnChange and using a ; between them. So can you help me make the code that you supplied not field specific. Basically I have to run this code for each dropdown box I have in the form.

Each dropdown will have a hidden field, and they will be named like this:

Dropdown is named selColor
Hidden for this will be selColorH

 
If they are all select lists, you can alter it like this

Code:
<form name="form1" method="post" action="displaycolor.asp">
  <select name="selColor" size="1" onChange="showColor([blue]this[/blue])" [blue]id="selColor"[/blue]>
    <option value="1">Green</option>
    <option value="2">Yellow</option>
    <option value="3">Blue</option>
    <option value="4">Red</option>
  </select>
  <input type=hidden name=[blue]selColorH[/blue]>
</form>
<script>
function showColor([blue]inObj[/blue]){
      selIndex = [blue]inObj[/blue].selectedIndex
    selColor = [blue]inObj[/blue].options[selIndex].text
    alert(selColor)
    //to pass it 
    [blue]hiddenCell = document.getElementById(inObj.id + "H")
    hiddenCell.value = selColor[/blue]
}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top