I love developing usable and rich web applications using the latest techniques available.

Updating multiple fields with Script.aculo.us Autocomplete

November 5, 2006

When working on an application i needed an autocompleted input field. Because i allready was using Script.aculo.us for some effects i decided to use the ajax.autocompleter function.

The one big problem i faced is that i needed to update multiple fields, unfortunately the autocompleter function only updates one inputfield.

So i started working on a way to update multiple input fields, which i’m going to share with you.

Let’s get started.

First of all get your copy of prototype.js and script.aculo.us library’s and include these in a html file called index.html.
Next thing is to create our form

Autocomplete

Add these lines to the head of your html file

Now make a new html file called “data.html” and paste these lines in it

  • Result One

    This is result one
  • Result Two

    This is result Two
  • Result Three

    This is result Three

Finally we’re going to do some basic css styling

div.autocomplete {
position:absolute;
width:250px;
background-color:white;
border:1px solid #888;
margin:0px;
padding:0px;
}
div.autocomplete ul {
list-style-type:none;
margin:0px;
padding:0px;
}
div.autocomplete ul li.selected { background-color: #ffb;}
div.autocomplete ul li {
list-style-type:none;
display:block;
margin:0;
padding:2px;
height:32px;
cursor:pointer;
}

Now your basic autocompleter should be working, time for the next steps.

We’ll create some input fields that we want to get updated after a result is selected.
Add this fieldset in the form we created earlier

More values:

Add the following option to the Autocompleter options, this will call the update function, after you selected a result.

afterUpdateElement: updateFields
[/source:javascript]
Like this:
[source:javascript]

Now we need to update the results file

  • Result One

    Car
    Bycicle
    Bus
    Boat

  • Result Two

    Clouds
    Rain
    Water
    Lightning

  • Result Three

    Earth
    Wind
    Fire
    More…

And the final thing to add is the update function

function updateFields(element, selectedElement) {
var oResult = selectedElement.childNodes.item(1);
$(“a”).value = oResult.childNodes.item(1).innerHTML;
$(“b”).value = oResult.childNodes.item(3).innerHTML;
$(“c”).value = oResult.childNodes.item(5).innerHTML;
$(“d”).value = oResult.childNodes.item(7).innerHTML;
}

Now we’re done, the Autocompleter will update 4 extra input fields with data.

Check my personel testcase for a working sample.

There’s probably a better solution to solve this problem, but for now it works fine.
I’m planning to make a more dynamic way of updating fields in the near future, because it’s pretty static this way.

If you have any suggestions or i forgot something in my post please leave a comment.

Bookmark and Share
Filed under: General

19 Comments »

  1. Thanks for this! It really solved a problem for me.

    12/12/2006 @ 4:58 pm — J Mitchell
  2. This tutorial sucks balls.

    15/06/2007 @ 9:27 pm — praxedis
  3. Praxedis;
    Some feedback could help

    18/06/2007 @ 11:06 amEelco Wiersma
  4. Hi, I see that in your example is working but I created the same exaple like you write in this article and is not working. I have an eror when add the text in the controle “Page can not be display!” The way for page is corectly but te aplication is not working. Can you help me with this?

    03/07/2007 @ 3:50 pm — real4ever
  5. It’s been a while since I wrote this post and it’s probably already outdated, I’m not sure it works with newer versions of script.aculo.us.

    Could you put your code online so I can have a look at it?

    07/07/2007 @ 12:24 amEelco Wiersma
  6. This does the same as my code does. In IE it updates the values but not with the correct values as in FireFox.

    If anybody can give me any pointers it would be appreciated.

    18/07/2007 @ 10:23 am — Carlo-Hein Versfeld
  7. Very elegant solution, thank you very much!.

    16/01/2008 @ 11:36 am — patrick
  8. How do I present the suggest lists just displaying two of these fields
    eg Result Two, apples

    I would like to build a people search which just displays name,phone but carries address,first,last-name,id etc..

    22/01/2008 @ 2:55 am — Paul
  9. Is not working!
    Not even your example.. Autocomplete is suggesting the same item three times (id = 1 ; with Mozilla..)
    Try to take a direct look at it’s suggesting script:
    http://www.edouble-u.nl/testcases/autocomplete/result.php
    :)

    01/02/2008 @ 9:33 pmanutu.ro
  10. I camed back to appologize and to tell you why it’s not working (when we follow the above steps..).
    Insted of the above suggesting_results page, use this and it will work:

    Result One

    Car
    Bycicle
    Bus
    Boat

    Result Two

    Clouds
    Rain
    Water
    Lightning

    Result Three

    Earth
    Wind
    Fire
    More…

    The problem was that the did not contained all the other spans..

    01/02/2008 @ 10:25 pmanuntu.ro
  11. I re-re-camed back because the blog’s editor striped the html code..
    Insted of the above suggesting_results page, use this and it will work:

    Result One

    Car
    Bycicle
    Bus
    Boat

    Result Two

    Clouds
    Rain
    Water
    Lightning

    Result Three

    Earth
    Wind
    Fire
    More...

    The problem was that the

    did’t contained all the other spans..

    01/02/2008 @ 10:29 pmanuntu.ro
  12. Result One

    Car
    Bycicle
    Bus
    Boat

    Result Two

    Clouds
    Rain
    Water
    Lightning

    Result Three

    Earth
    Wind
    Fire
    More…

    01/02/2008 @ 10:30 pmanuntu.ro
  13. <ul>
    <li>Result One
    <span class=\"informal\">
    <span class=\"a\">Car</span>
    <span class=\"b\">Bycicle</span>
    <span class=\"c\">Bus</span>
    <span class=\"d\">Boat</span>
    </span>
    </li>

    <li>Result Two
    <span class=\"informal\">
    <span class=\"a\">Clouds</span>
    <span class=\"b\">Rain</span>
    <span class=\"c\">Water</span>
    <span class=\"d\">Lightning</span>
    </span>
    </li>

    <li>Result Three
    <span class=\"informal\">
    <span class=\"a\">Earth</span>
    <span class=\"b\">Wind</span>
    <span class=\"c\">Fire</span>
    <span class=\"d\">More…</span>
    </span>
    </li>
    </ul>

    01/02/2008 @ 10:34 pmanuntu.ro
  14. <ul>
    <li>Result One
    <span class="informal">
    <span class="a">Car</span>
    <span class="b">Bycicle</span>
    <span class="c">Bus</span>
    <span class="d">Boat</span>
    </span>
    </li>

    <li>Result Two
    <span class="informal">
    <span class="a">Clouds</span>
    <span class="b">Rain</span>
    <span class="c">Water</span>
    <span class="d">Lightning</span>
    </span>
    </li>

    <li>Result Three
    <span class="informal">
    <span class="a">Earth</span>
    <span class="b">Wind</span>
    <span class="c">Fire</span>
    <span class="d">More…</span>
    </span>
    </li>
    </ul>

    01/02/2008 @ 10:37 pmanuntu.ro
  15. thanks anuntu.ro, the code in this article is already outdated and could be done in a much better manner, but i’m glad it helped you:)

    08/02/2008 @ 3:02 pmEelco Wiersma
  16. very very great solutions but not worked in IE … oResult.childNodes.item(1).innerHTML is undefined!!!

    19/02/2008 @ 8:07 pm — simone.garza
  17. please eelco….what is the much better manner?? worked for IE too

    19/02/2008 @ 8:08 pm — simone.garza
  18. simone, unfortunately it’s been a long time I worked with script.aculo.us. It did work with IE as far as I can recall.

    25/02/2008 @ 11:00 pmEelco Wiersma
  19. Sweet resource, just what I have been looking for.

    28/03/2008 @ 12:00 amBubbila

Leave a comment