...

View Full Version : Textbox value from option name or id



Datis
09-04-2011, 01:41 PM
Hey guys, I recently found a small code in internet which use option value in select tag to insert value in text box, so text box has the exact same value as selected option:




<form name="theform" onsubmit="CheckForm()">

<select name="myOptions" onchange="document.theform.showValue.value=this.value">


<option name="(some dynamic text)" id="(some dynamic text)" value="I am the first option">I am the first option</option>
<option name="(some dynamic text)" id="(some dynamic text)" value="I am the second option">I am the second option</option>
<option name="(some dynamic text)" id="(some dynamic text)" value="I am the third option">I am the third option</option>
<option name="(some dynamic text)" id=""(some dynamic text)" value="I am the third option">I am the fourth third option</option>

</select>
<input type="text" name="showValue"><br>
</form>



Now what I want is simple and a little different from this one, I want the text box value to be as exact as selected option name or id (doesn't matter which one) which means if an option selected, text box value has to follow that option name or id.

Thanks folks.

Philip M
09-04-2011, 04:47 PM
That is not the right way to go about it! Names and ids do not contain text.


<form name="theform" onsubmit="CheckForm()">

<select name="myOptions" onchange="showBox()">
<option>I am the first option</option>
<option>I am the second option</option>
<option>I am the third option</option>
<option>I am the fourth option</option>

</select>
<input type="text" name="showValue" value = "Some Dynamic Text One" size = "50"><br>
</form>

<script type = "text/javascript">
function showBox() {
var texts = [];
texts[0] = "Some Dynamic Text One";
texts[1] = "Some Dynamic Text Two";
texts[2] = "Some Dynamic Text Three";
texts[3] = "Some Dynamic Text Four";
var x = theform.myOptions.selectedIndex;
document.theform.showValue.value = texts[x];
}
</script>


The Pakistanis have a natural ability to commit suicide - BBC Radio cricket commentator

Datis
09-08-2011, 09:18 AM
That is not the right way to go about it! Names and ids do not contain text.


<form name="theform" onsubmit="CheckForm()">

<select name="myOptions" onchange="showBox()">
<option>I am the first option</option>
<option>I am the second option</option>
<option>I am the third option</option>
<option>I am the fourth option</option>

</select>
<input type="text" name="showValue" value = "Some Dynamic Text One" size = "50"><br>
</form>

<script type = "text/javascript">
function showBox() {
var texts = [];
texts[0] = "Some Dynamic Text One";
texts[1] = "Some Dynamic Text Two";
texts[2] = "Some Dynamic Text Three";
texts[3] = "Some Dynamic Text Four";
var x = theform.myOptions.selectedIndex;
document.theform.showValue.value = texts[x];
}
</script>


The Pakistanis have a natural ability to commit suicide - BBC Radio cricket commentator

Thanks Philip but that's not what i really meant, you see the options are dynamic (connected to db and table) and they'll be repeated so we dont really have four options it could be 50 options or 75 options (based on what admin added) anyway the select tag and its repeating options value contain int (number) in db and that text field has to contain a title for that int so i cant get options value as text box one because it'll contain numbers too, for example option no.6 is Brazil (Brazil id lets is 10). also titles(option labels) are dynamic too so they'll be repeated along side of their values.

I'm going to confuse you here so the above description needs option name id or labels to set as text box value (maybe we could go with option labels)?! so forget name and id, could we just set text box value with chosen option label. the code in my first post equal chosen option value with text box value.

Thanks

Datis
09-09-2011, 09:25 AM
resolved, thank guys.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum