Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple auto-complete fields with single soruce

Posted on 2013-01-02
4
Medium Priority
?
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

609 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