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 6330575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:53:47+00:00 2026-05-24T17:53:47+00:00

The title is a guess as to what is going wrong with my script:

  • 0

The title is a guess as to what is going wrong with my script:

This is in my global.js script:

alert(search.getLabelsNames(); //alerts as undefined.
$('#search').autocomplete({
    source: function( request ) {
        search.getLabelsNames();
    },
    minLength:1
});

This is in my functions.js script:

var search;
window.search = {
    getLabelsNames:function( search ) {
        $.ajax({
            url : '../db_scripts/get_labels_names.php',
            type: "POST",
            data: { id: search }, //this defaults to nothing. not a problem
            success : function( data ) {
                var dataObj = jQuery.parseJSON( data );
                $.each(dataObj, function() {
                    alert(this.name);
                    return this.name;
                })
            }
        });
    }
}

In the each function, this.name, returns every one of the label names correctly from the database. But when I call it from globals.js, it returns as undefined. If I return the number 1, the search.getLabelsNames() alerts 1.. so it has no problem finding the global function.

what is wrong with this script and why can’t global.js find the this.name that is being returned?

  • 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-05-24T17:53:49+00:00Added an answer on May 24, 2026 at 5:53 pm

    You have two problems:

    1. You cannot return data from a jQuery.each callback. The return value indicates whether to stop the iteration or not. From the documentation:

      We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

    2. You cannot return data from an Ajax callback. Ajax is asynchronous, which means in your case that getLabelsNames returns before the response is retrieved and processed.

    Luckily, the autocomplete plugin accepts a function as source. You are already using it, but not correctly. Make it accept the second argument, which is a callback. From the documentation:

    The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

    • A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo".

    • A response callback, which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties). It’s important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.

    So what you have you have to do is passing this callback along to getLabelsNames so that it can be called in the success method of the Ajax call:

    $('#search').autocomplete({
        source: function(request, callback) {
            // pass the callback along
            search.getLabelsNames(request.term, callback);
        },
        minLength:1
    });
    
    window.search = {
        getLabelsNames:function(search, callback) { // accept callback as argument
            $.ajax({
                url : '../db_scripts/get_labels_names.php',
                type: "POST",
                data: { id: search }, //this defaults to nothing. not a problem
                success : function( data ) {
                    // format the data
                    data = $.map(jQuery.parseJSON(data), function(obj) {
                        return obj.name;
                    });
                    // pass the data to the callback
                    callback(data);
                }
            });
        }
    }
    

    Note that I use jQuery.map [docs] here, where returning a value form the callback has a different meaning. Don’t confuse it with jQuery.each.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is in the title I guess. This is the temporary solution I
I guess the title of this questions says it all, or says nothing... After
I guess the question title sums it up. Is there a time when it
As you can guess from the title, the outlining with regions finally made me
Sorry for the long question title. I guess I'm on to a loser on
Umm, I guess my questions in the title: How do I turn on Option
Title is the entire question. Can someone give me a reason why this happens?
I guess the title almost says it all. My original url looks like: http://www.something.com/buy/index.php?p=025823
Ok... so i guess the title is a bit confusing. so i will explain:
I guess I don't give title nicely, anyways, I can explain it better here:

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.