1

I am using JQuery Autocomplete, and want to populate it from a JSON Array read from a hidden Label.

This code:

string[] tags = { "Apple", "Orange", "Banana", "Lychee", "Pear", "Lemon" };
var json = JsonConvert.SerializeObject(tags);
lblJson.Text = json;

produces this result:

enter image description here

I am trying to then populate the autocomplete source with this array like this:

var availableTags = $("#lblJson").text();
$("#tbTag").autocomplete({
  source:availableTags
});

but my autocomplete doesn't, err, autocomplete.

However, if I copy the JSON array directly into the Javascript code it works fine:

var availableTags = ["Apple", "Orange", "Banana", "Lychee", "Pear", "Lemon"];
$("#tbTag").autocomplete({
  source:availableTags
});

1 Answer 1

3
var availableTags = $("#lblJson").text();

The availableTags you got here is not an array, but rather a string that contains a json array.

Since you are using jQuery, you can use this to parse it:

var json = $("#lblJson").text();
var availableTags = $.parseJSON(json);
Sign up to request clarification or add additional context in comments.

1 Comment

Legend! I've been banging my head on this one! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.