Changing the value of texfiled using onChange?

Y

Young-Soo Roh

Hi.
I need to get simple script working.
I have many select object.
When I select a different option, I would like to change the value of
corresponding text input field.
How can I do this using javascript?

Here is what I have so far.

<SELECT NAME=\"%s\" onChange=\"fill_desc(this.form, $name, $field_name)>

<script language="javascript">
<!-- Hide me please!
function fill_desc(form, name, field_name) {
var myindex=form.name.selectedIndex
if(form.name.options[myindex].value != "0") {
form.field_name.value = form.name.options[myindex].value;
}
}
//-->

Thanks for your help.
 
L

Lee

Young-Soo Roh said:
Hi.
I need to get simple script working.
I have many select object.
When I select a different option, I would like to change the value of
corresponding text input field.
How can I do this using javascript?

Here is what I have so far.

<SELECT NAME=\"%s\" onChange=\"fill_desc(this.form, $name, $field_name)>

You seem to be missing a closing \" in that line, but it looks like
you've only shown part of it, so that's probably not really a problem.
<script language="javascript">
<!-- Hide me please!
function fill_desc(form, name, field_name) {
var myindex=form.name.selectedIndex
if(form.name.options[myindex].value != "0") {
form.field_name.value = form.name.options[myindex].value;
}
}
//-->

You don't need to hide code from any browser you're going to run into.
The part of a reference that follows a "." cannot be a variable.
If name is a variable, you can't refer to it as "form.name".
You refer to it as form.elements[name], or you simply pass a reference
to the Select object itself, and use its form attribute to refer to
the form that contains it:

<SELECT NAME=\"%s\" onChange=\"fill_desc(this,$field_name)>

<script type="text/javascript">
function fill_desc(select_reference,field_name){
if(select_reference.selectedIndex){ // true if not 0
select_reference.form.elements[field_name].value=
select_reference.options[select_reference.selectedIndex].value;
}
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top