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

  • Home
  • SEARCH
  • 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 8531313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:27:20+00:00 2026-06-11T09:27:20+00:00

I am using the Typeahead component of Twitter Bootstrap 2.1.1, and jQuery 1.8.1 I

  • 0

I am using the Typeahead component of Twitter Bootstrap 2.1.1, and jQuery 1.8.1

I am trying to access the text box element from within typeahead’s updater function. Here is my current code, which works great:

$('#client-search').typeahead({
    source: function (query, process) {
        $.get(url, { query: query }, function (data) {
            labels = [];
            mapped = {};
            $.each(data, function(i,item) {
                mapped[item.label] = item.value;
                labels.push(item.label);
            });
            process(labels);
        });
    }
    ,updater: function (item) {
        $('#client-id').val(mapped[item]);
        return item;
    }
    ,items: 10
    ,minLength: 2
});

I have many typeahead search boxes on the same page. Each search box has an id #xxx-search and a corresponding hidden input with id #xxx-id. I’d like to re-use the same code for everything by doing something like this:

$('#client-search, #endClient-search, #other-search').typeahead({

    ...

    ,updater: function (item) {
        var target = $(this).attr('id').split('-')[0] + '-id';
        $('#'+target).val(mapped[item]);
        return item;
    }

    ...

I thought that this would refer to the text box element in use, but apparently not, because I get an undefined error in firebug:

$(this).attr("id") is undefined

When I use this instead of $(this), I get:

this.attr is not a function

Can anyone help make this work?


UPDATE: THE ANSWER, AND MORE!

Thanks to benedict_w’s answer below, not only does this work perfectly now, but I have also made this much better in terms of re-usability.

Here is what my <input>s look like:

<input type="text" autocomplete="off"
    id="client-search"
    data-typeahead-url="/path/to/json"
    data-typeahead-target="client-id">
<input type="hidden" id="client-id" name="client-id">

Notice how the search box references the hidden id. Here’s the new js:

$(':input[data-typeahead-url]').each(function(){
    var $this = $(this);
    $this.typeahead({
        source: function (query, process) {
            $.get($this.attr('data-typeahead-url'), { query: query }, function (data) {
                labels = [];
                mapped = {};
                $.each(data, function(i,item) {
                    mapped[item.label] = item.value;
                    labels.push(item.label);
                });
                process(labels);
            });
        }
        ,updater: function (item) {
            $('#'+$this.attr('data-typeahead-target')).val(mapped[item]);
            return item;
        }
        ,items: 10
        ,minLength: 2
    });
});
  • 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-11T09:27:21+00:00Added an answer on June 11, 2026 at 9:27 am

    You are inside another function so you need to save a reference to the element in the outer closure, this can then be passed into the event handler of the inner closure. You can do that using an $.each() call to loop through the selector saving a reference to the element (this) each time – e.g.

    $('#client-search, #endClient-search, #other-search').each(function() {
        var $this = $(this);
        $this.typeahead({
            ...
    
            ,updater: function (item) {
                var target = $this.attr('id').split('-')[0] + '-id';
                $('#'+target).val(mapped[item]);
                return item;
            }
            ...
         });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Twitter Bootstrap with Jquery. I want to use the TYPEAHEAD function for
I am using the typeahead js extension from twitter bootstrap for an autocomplete field.
using twitter bootstrap typeahead i get google chrome suggestion list above typeahead list $(document).ready(function
I'm using the rottentomatoes movie API in conjunction with twitter's typeahead plugin using bootstrap
I'm using the Twitter bootstrap-typeahead plugin with an HTML form: http://twitter.github.com/bootstrap/javascript.html#typeahead which is working
Using Bootstrap's typeahead javascript plugin, I'm attempting to change the data-source attribute via jQuery's
I'm using this fork of the Twitter Bootstrap typeahead library , which allows asynchronous
I'm using twitter bootstrap in my application. I want to use the typeahead in
i'm using twitter bootstrap 2.0.4. i want to populate the dropdown using ajax. the
Using CMake I want to check if a particular function (cv::getGaborKernel) from OpenCV library

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.