Changing the value of texfiled using onChange?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Young-Soo Roh

    Changing the value of texfiled using onChange?

    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="javas cript">
    <!-- Hide me please!
    function fill_desc(form, name, field_name) {
    var myindex=form.na me.selectedInde x
    if(form.name.op tions[myindex].value != "0") {
    form.field_name .value = form.name.optio ns[myindex].value;
    }
    }
    //-->

    Thanks for your help.


  • Lee

    #2
    Re: Changing the value of texfiled using onChange?

    Young-Soo Roh said:[color=blue]
    >
    >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
    >correspondin g 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)>[/color]

    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.
    [color=blue]
    ><script language="javas cript">
    ><!-- Hide me please!
    >function fill_desc(form, name, field_name) {
    > var myindex=form.na me.selectedInde x
    > if(form.name.op tions[myindex].value != "0") {
    > form.field_name .value = form.name.optio ns[myindex].value;
    > }
    >}
    >//-->[/color]

    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,$fie ld_name)>

    <script type="text/javascript">
    function fill_desc(selec t_reference,fie ld_name){
    if(select_refer ence.selectedIn dex){ // true if not 0
    select_referenc e.form.elements[field_name].value=
    select_referenc e.options[select_referenc e.selectedIndex].value;
    }
    }
    </script>

    Comment

    Working...