Closed Thread Icon

Topic awaiting preservation: How to show select option value in text box? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8710" title="Pages that link to Topic awaiting preservation: How to show select option value in text box? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: How to show select option value in text box? <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-11-2003 05:19

Hi, guys. How are you?
It turned out beatiful day though. It was bloody cold this morning.
Well, may I ask you something, please?
Would you know how to show select option value of the form into text box?
I wanted to show each "month" into the text box below the select option drop down.
Its drop down includes like Junuary, Februry.....December.
What I did was:

code:
document.theForm.result.value=document.theForm.month.selectedIndex +"Year " + Result;



But as you know it shows like 0, 1, 2, 3, 4, 5, ....12. I found selectedIndex shows each option's index. Not value itself. How can I show value itself?

I did like:

code:
function cal(){

var Year=parseInt(document.theForm.year.value);
Result=Year+20;
theMonth=document.theForm.month.selectedIndex;

switch(theMonth)
{
case 0:
document.theForm.result.value=" January Year " + Result;
break
case 1:
document.theForm.result.value=" February Year " + Result;
break
case 2:
document.theForm.result.value=" March Year " + Result;
break
case 3:
document.theForm.result.value=" April Year " + Result;
break
case 4:
document.theForm.result.value=" May Year " + Result;
break
case 5:
document.theForm.result.value=" June Year " + Result;
break
case 6:
document.theForm.result.value=" July Year " + Result;
break
case 7:
document.theForm.result.value=" August Year " + Result;
break
case 8:
document.theForm.result.value=" September Year " + Result;
break
case 9:
document.theForm.result.value=" October Year " + Result;
break
case 10:
document.theForm.result.value=" November Year " + Result;
break
case 11:
document.theForm.result.value=" December Year " + Result;
break
}

}



But I guess there will be better way to do so. Would you know something about this???

Hiroki Kozai

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 06-11-2003 09:32

To get at the different things in a drop-down select you have this to play with:

document.forms[0].dropDownBox[index].text gives you the displayed text
document.forms[0].dropDownBox[index].value gives you the value
document.forms[0].dropDownBox.options.selectedIndex.text gives you the selected text
document.forms[0].dropDownBox.options.selectedIndex.value gives you the selected value


This snip loops the select box and alerts you with all combinations

code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>LoopDaDropBox</TITLE>

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- ;
function popEm(){
thing = document.forms[0].dropBox;
for(i=0;i<thing.length;i++){
alert(thing[i].value);
alert(thing[i].text);
}
alert("selected value is: " + thing.options.selectedIndex.text);
}
// -->
</SCRIPT>
</HEAD>

<BODY>

<FORM ACTION="#" >
<SELECT NAME="dropBox" onchange="popEm()">
<OPTION value="0"> - Select value - </OPTION>
<OPTION value="1">Value 1</OPTION>
<OPTION value="2">Value 2</OPTION>
<OPTION value="3">Value 3</OPTION>
</SELECT>
</FORM>

</BODY>
</HTML>



/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-11-2003 17:20
quote:
document.forms[0].dropDownBox[index].text gives you the displayed text
document.forms[0].dropDownBox[index].value gives you the value
document.forms[0].dropDownBox.options.selectedIndex.text gives you the selected text
document.forms[0].dropDownBox.options.selectedIndex.value gives you the selected value



I believe you meant:

document.forms[0].dropDownBox.options[index].text gives you the displayed text
document.forms[0].dropDownBox.options[index].value gives you the value
document.forms[0].dropDownBox.options[document.forms[0].dropDownBox.selectedIndex]text gives you the selected text
document.forms[0].dropDownBox.options[document.forms[0].dropDownBox.selectedIndex]value gives you the selected value

selectedIndex is an integer, not an object. The drop down box has an "options" array, which is a list of all of the possible options. By retriving the selectedIndex'th entry of the array, you can get information about the selected option.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-11-2003 23:34

Hi, Dms, Hi, Slime.
Many many thanks for your replies.
How are you today?
My first job of this morning was to fix my this code.
I am still cofused about this.
I did this:

code:
document.theForm.result.value=document.theForm.dropDownBox.options[document.forms[0].dropDownBox.selectedIndex].value +"Year " + Result;



Instead of:

code:
document.theForm.result.value=document.theForm.month.selectedIndex +"Year " + Result;



But I am afraid still doesn't work.....
Did I do wrong way???
HHmmmmm....



Hiroki Kozai

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 06-12-2003 17:25

oops... sorry 'bout that
Slime is of course absolutley right!
(And I'm trying to do too many things at the same time...)
Sigh, when will I learn...
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-12-2003 19:45

Use .text instead of .value.

If the option element looks like this:

<option value="something">choice</option>

then .value will be "something" and .text will be "choice".

mob68com
Bipolar (III) Inmate

From: Born in Dublin, Ireland .:. now living in the US
Insane since: May 2003

posted posted 06-14-2003 05:03

I had a few beers, so I'm probably not following this correctly. You want to show the "Select Option" without it having a link? Correct? Maybe, I don't know. Anyway, just use # for the option and it will stay where it is. Ok, back to the beer, it's a nice rainy night out, what else am I going to do?
Hic***

« BackwardsOnwards »

Show Forum Drop Down Menu