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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:19:58+00:00 2026-06-02T02:19:58+00:00

jQuerys .autocomplete() function has two variants for it’s ‘source’ option. An Array of Strings:

  • 0

jQuerys .autocomplete() function has two variants for it’s ‘source’ option.

An Array of Strings:

[ "Choice1", "Choice2" ]

An Array of Objects with label and value properties:

 [ { label: "Choice1", value: "value1" }, ... ]

These both work fine when passed directly to the .autocomplete() function:

$('input').autocomplete({source: [ "Choice1", "Choice2" ], minLength: 0});

OR

$('input').autocomplete({source: [ { label: "Choice1", value: "value1" } ], minLength: 0});

BUT I can’t get either to work when passed as variables:

var source = '[ "Choice1", "Choice2" ]';
$('input').autocomplete({source: source, minLength: 0});

This is to do with implicit casting of the variable to a string – autocomplete treats the values from the variable identically to how it handles an AJAX source (which is a string).

Ultimately I want to use a JSON value from a data-json attribute of the input element.

For added interest:

$('input').autocomplete({source: $('input').data('json'), minLength: 0});

works perfectly for the first variant, but reverts to the string behavior for the second variant.

Using $('input').attr('data-json') doesn’t work for either variant.

A ‘hack’ which does work for both variants is to do this:

$('input').autocomplete({source: eval($('input').attr('data-json')), minLength: 0});

Did I miss something which would allow .data() to return the apparently valid JSON in the second variant as an object?

Is there another way, or do I just need to make peace with eval() despite all the wisdom around that it’s probably not a good thing to do?

Thanks for your thoughts..

  • 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-02T02:20:01+00:00Added an answer on June 2, 2026 at 2:20 am
    var source = '[ "Choice1", "Choice2" ]';
    

    That is a String, not an array.

    Try without quotes:

    var source = [ "Choice1", "Choice2" ];
    

    If you find yourself in the situation of having to get the data from a JSON string, don’t use eval (it’s evil), use JSON.parse (which all modern browsers have)

    var source = JSON.parse( '[ "Choice1", "Choice2" ]' ); 
    

    or, since you are already using jQuery

    var source = $.parseJSON( '[ "Choice1", "Choice2" ]' ); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Jquery autocomplete ( LINK) has an option to turn it off, which looks
I've got this code of autocomplete jqueryUI: .autocomplete({ source: function( request, response ) {
I've got a problem with the jquery autocomplete function. If I specify a source
I am trying to create an autocomplete field with jquerys famous autocomplete and by
jQuery autocomplete docs state there should be two Models, the first being the model
I have a jQuery Autocomplete field on an ASP.Net Webform and everything has been
I'm using the JQuery autocomplete plug in, and am passing in an array of
When I use JQuery's autocomplete and hardcode the array values in the page it
I've been using the JQuery Autocomplete plugin with JQuery version 1.3.2, and it has
I have two jQuery UI Autocomplete widgets set up. With the first autocomplete, the

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.