Solved

Multiple auto-complete fields with single soruce

Posted on 2013-01-02
4
385 Views
Last Modified: 2013-01-07
Below is code for a jquery autocomplete and it works fine for the user to select a single country. However, we want the user to match multiple countries using the same autocomplete source.

Without hardcoding  and copying the jquery portion using a difference (ie. country-1, country-2) for the source (a difference in the field name is okay) , how can we use the same jquery portion (source) for multiple form fields ?

Example (user input does autocomplete from same source):

1) Picture of US flag, input for user text of name of country.
2) Picture of UK flag, input for user text of name of country.
3) Picture of China flag, input for user text of name of country.
4) Picture of Mexico flag, input for user text of name of country.


Currently:
\$( "#country-name" ).autocomplete({
            source: "country-search.pl",
            minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        \$("#country-name").val(ui.item.label);
        \$("#country").val(ui.item.value);
    },
    focus: function(event, ui) {
        event.preventDefault();
        \$("#country-name").val(ui.item.label);
                               }
        });

Open in new window


<input type="hidden" value="" name="country" id="country"><input name="country-name" id="country-name" value=""/>

Open in new window



Desired (all the country fields use country-search.pl for autocomplete):
\$( "#country-name" ).autocomplete({
            source: "country-search.pl",
            minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        \$("#country-name").val(ui.item.label);
        \$("#country").val(ui.item.value);
    },
    focus: function(event, ui) {
        event.preventDefault();
        \$("#country-name").val(ui.item.label);
                               }
        });

Open in new window


<input type="hidden" value="" name="country-1" id="country-1"><input name="country-name-1" id="country-name-1" value=""/>
<input type="hidden" value="" name="country-2" id="country-2"><input name="country-name-2" id="country-name-2" value=""/>
<input type="hidden" value="" name="country-3" id="country-3"><input name="country-name-3" id="country-name-3" value=""/>
<input type="hidden" value="" name="country-4" id="country-4"><input name="country-name-4" id="country-name-4" value=""/>

Open in new window

0
Comment
Question by:freshgrill
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Use :
$( "[id^='country-name']" ).autocomplete({ // better to use a class here...
            source: "country-search.pl",
            minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        $(event.target).val(ui.item.label);
        $(event.target).val(ui.item.value);
    },
    focus: function(event, ui) {
        event.preventDefault();
        $(event.target).val(ui.item.label);
                               }
        });

Open in new window

0
 

Author Comment

by:freshgrill
Comment Utility
@ leakim971: it is now showing all the autocomplete for all the fields, but when I select the country the selected country is now showing up.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
please provide a link to see your page.
0
 

Author Closing Comment

by:freshgrill
Comment Utility
I had to remove the select and focus portions - but then it all worked.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now