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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:59:57+00:00 2026-05-24T20:59:57+00:00

I’m fairly new to javascript. I downloaded some code to make an autocompleter for

  • 0

I’m fairly new to javascript. I downloaded some code to make an autocompleter for search boxes, but it’s not working as I expected it.

This is the code that I downloaded:

Autocompleter.Local = new Class({

    Extends: Autocompleter,

    initialize: function (element, tokens, options) {
        this.parent(element, options);
        console.log(tokens); // I added this
        this.tokens = tokens;
    },

    query: function () {
        console.log(this.tokens); // and this
        this.update(this.filter());
    }

});

To make use of this code, I do this:

var Activities = ['aa', 'bb', 'cc', 'abcd'];

function InitializePage() {

    new Autocompleter.Local('txtSearch', Activities, {
        'minLength': 1, // We wait for at least one character
        'overflow': false, // Overflow for more entries
        'selectMode': 'type-ahead'
    });
}

window.addEvent('domready', function () {
   InitializePage();
});

The tokens parameter for the initialize function is an array of all the possible things that the autocompleter might suggest. the filter() function filters these so only relevant ones are displayed to the user. What I want to do is change the tokens array at various times so different things are suggested to the user.

I tried doing this:

new Request.JSON({ method: 'get', url: 'handler/GetActivities.ashx', autoCancel: true, urlEncoded: false, secure: false,
    headers: { "Content-type": "application/json" },
    onSuccess: function (_json, _jsonText) {
        Activities = _json;
    }
}).send();

I have confirmed that the Activities variable is being updated by this request. However, the tokens variable inside the autocompleter class remains as the array that it was initialized as.

How do I get it so that the tokens variable points to the same data as the activities variable?

  • 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-24T20:59:58+00:00Added an answer on May 24, 2026 at 8:59 pm

    The problem is that you are overriding the array. You should replace the elements instead. You can add and remove elements in-place with splice [MDN]:

    Activities.splice(0, Activities.length, _json[0], _json[1], ...);
    

    This would remove all elements (from 0 to Activities.length) and add each element from the _json array.

    But _json might contain an unknown number of elements, and hardcoding it like we did above is very bad. Fortunately, we can use the apply [MDN] method to call a method with a variable number of arguments, provided as array:

    var params = [0, Activities.length].concat(_json);
    Activities.splice.apply(Activities, params);
    

    This is the equivalent to the call above, but adding all elements from _json.

    Reference: concat, splice, apply

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.