Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:59:38+00:00 2026-06-14T09:59:38+00:00

I have two text intputs, both have to run an autocompletion. The site is

  • 0

I have two text intputs, both have to run an autocompletion. The site is using Bootstrap, and the « typeahead » component. I have this HTML :

<input type="text" class="js_typeahead" data-role="artist" />
<input type="text" class="js_typeahead" data-role="location" />

I’m using the « data-role » attribute (that is sent to the Ajax controller as a $_POST index), in order to determine what kind of data has to be retrieved from the database.

The Javascript goes this way :

var myTypeahead = $('input.js_typeahead').typeahead({
 source: function(query, process){
 var data_role;
  data_role = myTypeahead.attr('data-role');
  return $.post('/ajax/typeahead', { query:query,data_role:data_role },function(data){
    return process(data.options);
  }); } });

With PHP, I check what $_POST['data-role'] contains, an run the MySQL query (in this case, a query either on a list of Artists, or a list of Locations).

But the problem is the second “typeahead” returns the same values than the first one (list of Artists). I assume it’s because the listener is attached to the object « myTypeahead », and this way the “data-role” attribute which is used, will always be the same.

I think I could fix it by using something like :
data_role = $(this).attr('data-role');
But of course this doesn’t work, as it’s a different scope.

Maybe I’m doing it all wrong, but at least maybe you people could give me a hint. Sorry if this has already been discussed, I actually searched but without success.

Thanks in advance, Clem (from France, sorry for my english)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:59:39+00:00Added an answer on June 14, 2026 at 9:59 am

    In this case you will need to iterate through the elements, getter methods only return the value of the first selected element.

    $('input.js_typeahead').each(function(){
        var myTypeahead = $(this).typeahead({
            source: function(query, process){
                var data_role;
                data_role = myTypeahead.attr('data-role');
                return $.post('/ajax/typeahead', { 
                    query:query,data_role:data_role 
                },function(data){
                    process(data.options);
                }); 
         } 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two in my html code: <input class=input1 type=text size=15 name=login> <input
How can i achieve this using jQuery. I have two text inputs on the
I have two text boxes t1 and t2 in an html page. I'd like
I have two text fields in my view. I did it using IB. My
I have two inputs (text fields) required by JQuery Plug-in : <input type=text name=first_name
Say you have two inputs: <input id=input1 type=text/> and <input id=input2 type=text/> such that
i have two form as following <form id=form1 action=/Account/form1> <input type=text name=baar /> <input
I have a form with two input fields and a display text div: <div
I have two code projects both using CXF to use web services. When I
I have two text input fields like so: <input type=text id=test1.../> <input type=text id=test2.../>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.