Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2003
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    auto fill text fields based on a dropdown selection

    Have searched high and low for Javascript that would allow me to auto-populate five or so text fields in a form with corresponding values based on selection made at the top of the form in a dropdown option box.

    Any directions would be welcome!

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    Port Huron, MI, U.S.A.
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function Y(W)
    {
    switch(W)
    {
    case 1:this.value='that';otherdude.value='eggplant';break;
    case 2:this.value='spatula';otherdude.value='gypsy';break;
    case 3:this.value='mud';otherdude.value='pie';break;
    default :alert('ERROR!');break;


    }
    }
    ...
    <select name="T" onChange="Y(this.options[this.options.selectedIndex].value)">
    <option value="1">A</option>

    <option value="2">B</option>

    <option value="3">C</option>
    <option value="4">D</option>
    </select>
    Oh, was I supposed to put something here? ........

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Code:
    function fillFields(oSel)
    {
      var oForm = oSel.form;
      switch (oSel.options[oSel.selectedIndex].value)
      {
        case '1':oForm.field1.value='blah1';oForm.field2.value='foo1';break;
        case '2':oForm.field1.value='blah2';oForm.field2.value='foo2';break;
        case '3':oForm.field1.value='blah3';oForm.field2.value='foo3';break;
      }
    }
    ...
    <form>
    <select name="T" onchange="fillFields(this)">
    <option value="1">item 1</option>
    <option value="2">item 2</option>
    <option value="3">item 3</option>
    </select>
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    </form>

  • #4
    Regular Coder
    Join Date
    Mar 2003
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks!

    Will give that a shot later today and send a response.

  • #5
    Regular Coder
    Join Date
    Mar 2003
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    awesome!!!



    thank you very much - the code works verrrry nicely!!

  • #6
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would you use this code when trying to populate the values dynamically from a database?

  • #7


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •