...

View Full Version : text field and select field dependence



asherinho
07-31-2008, 10:16 AM
Hi guys! I want to create a form whereby whenever a value is selected from a list of select field it's corresponding value should appear on the text field.
example,if the select field has a list of file numbers,then if a certain file number is selected the corresponding file name should apppear on the text field

rangana
07-31-2008, 10:20 AM
You should not cross-post. A response was made at your early thread:
http://www.codingforums.com/showthread.php?t=145533

Philip M
07-31-2008, 10:38 AM
<form name = "myForm">
<select name = "colour" style="width:160px" onchange = "writebox()">
<option value = "Choose a Colour">Select a colour</option>
<option value = "Red">Red</option>
<option value = "Blue">Blue</option>
<option value = "Green">Green</option>
<option value = "Yellow">Yellow</option>
</select>
<br><br>
<input type = "text" name = "txtfield" id = "txtfield" size = "20">
</form>

<script type = "text/javascript">
function writebox() {
var pick1 = document.myForm.colour.selectedIndex;
if (pick1 == 0) {alert ("You must make a selection")}
document.myForm.txtfield.value = document.myForm.colour.options(pick1).value;
}
</script>

1.2) Do not crosspost- CodingForums.com is divided into various categories.
When initiating a thread, always locate the most appropriate category, and post your
question only ONCE, in that category. Do not post the same question in multiple categories.

asherinho
07-31-2008, 11:15 AM
Its my first time in this forum guys.I tried to play with the codes U gave me but what i want is the value to be displayed on text field is different from the value selected from the select field which is different from what the codes u gave me is doing.I am a begginer to javascript

Philip M
07-31-2008, 12:28 PM
You are not very clear. Is this what you want?



<form name = "myForm">
<select name = "filename" style="width:160px" onchange = "writebox()">
<option value = "You must choose a file">Select a file</option>
<option value = "File 1 Name">File 1</option>
<option value = "File 2 Name">File 2</option>
<option value = "File 3 Name">File 3</option>
<option value = "File 4 Name">File 4</option>
</select>
<br><br>
<input type = "text" name = "txtfield" id = "txtfield" size = "20">
</form>


<script type = "text/javascript">
function writebox() {
var pick1 = document.myForm.filename.selectedIndex;
if (pick1 == 0) {alert ("You must make a selection")}
document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;
}
</script>

rangana
07-31-2008, 12:34 PM
Should be closing and opening bracket instead:


document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;


Maybe I got your point, what Philip M has given would meet your requirement, you just need to replace highlighted to the corresponding values you wish:


<option value = "You must choose a file">Select a file</option>
<option value = "File 1 Name">File 1</option>
<option value = "File 2 Name">File 2</option>
<option value = "File 3 Name">File 3</option>
<option value = "File 4 Name">File 4</option>

Philip M
07-31-2008, 02:02 PM
Should be closing and opening bracket instead:


document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;


How is that different from:

document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;


:confused::confused::confused:

vwphillips
07-31-2008, 02:24 PM
why not?


function writebox() {
document.myForm.txtfield.value = document.myForm.filename.value;
}

rangana
07-31-2008, 02:25 PM
How is that different from:

document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;


:confused::confused::confused:

It only works on IE. :eek:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum