Changing the value of texfiled using onChange?

Discussion in 'Javascript' started by Young-Soo Roh, Oct 25, 2003.

  1. 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.
     
    Young-Soo Roh, Oct 25, 2003
    #1
    1. Advertisements

  2. Young-Soo Roh

    Lee Guest

    Young-Soo Roh said:
    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.
    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>
     
    Lee, Oct 25, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.