Solved

Multiple auto-complete fields with single soruce

Posted on 2013-01-02
4
386 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
ID: 38738487
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
ID: 38738623
@ 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
ID: 38738658
please provide a link to see your page.
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

947 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

19 Experts available now in Live!

Get 1:1 Help Now