Forum Moderators: open

Message Too Old, No Replies

JS - Specific value dynamically placed in readonly form field

New to javascript, this is probably a simple task.

         

dolphinsdock

7:24 pm on May 6, 2005 (gmt 0)

10+ Year Member



I'm brand new to javascript writing and I'm attempting to create a form in which a readonly text field has a specific value dynamically inserted into it depending on the selection in a dropdown menu. If I wasn't able to clearly describe it, I think you'll be able to figure out what I'm attempting to do by looking at my code below.


<HTML>
<HEAD>
<TITLE>Title</TITLE>

<script language="javascript" type="text/javascript">
function update()
{
f1=document.form.condition.value;

if(f1 == "condition1")
document.form.result.value='Result 1';
if(f1 == "condition2")
document.form.result.value='Result 2';
if(f1 == "condition3")
document.form.result.value='Result 3';
}
</script>

</HEAD>

<body>

<form name="form" action="handler.asp" method="post">
<select name="condition" size="1" onChange="update()">
<option selected disabled>------</option>
<option value="condition1">First Condition</option>
<option value="condition2">Second Condition</option>
<option value="condition3">Third Condition</option>
</select>

<input type="text" size="20" name="result" readonly value="-----">
</form>

</body>
</HTML>

gph

9:40 pm on May 6, 2005 (gmt 0)

10+ Year Member



I didn't change much, you were close.

What I did change was to send the option value as an argument for the function, removed language="javascript" because it's outdated and made onChange lower case. Lower case isn't required, it just works with any doctype.

There's slightly better ways to write your function but yours is easy for you to understand.

<HTML>
<HEAD>
<TITLE>Title</TITLE>

<script type="text/javascript">
function update(f1)
{
if(f1 == "condition1")
document.form.result.value='Result 1';
if(f1 == "condition2")
document.form.result.value='Result 2';
if(f1 == "condition3")
document.form.result.value='Result 3';
}
</script>

</HEAD>

<body>

<form name="form" action="handler.asp" method="post">
<select name="condition" size="1" onchange="update(this.options[this.selectedIndex].value)">
<option selected disabled>------</option>
<option value="condition1">First Condition</option>
<option value="condition2">Second Condition</option>
<option value="condition3">Third Condition</option>
</select>

<input type="text" size="20" name="result" readonly value="-----">
</form>

</body>
</HTML>