1

I am working on Jquery Autocomplete and due to internet poor connection, I have to load my whole list(I mean Json object) on page load itself.Now my question is how do I query on that Json object so my autocomplete get a filter as user start typing keyword in a textbox.

$(document).ready(function () {
    $("#InsuranceCompanyDisplayName").autocomplete({
        minLength: 2,
        source: function (request, response) {
            var data = $('#InsuranceCompanyjson').text();
            response($.map(data, function (item) {
                return {
                    value: item.InsuranceCompanyDisplay,
                    id: item.InsuranceCompanyId
                }
            }))
        },

    });

});

Here (var data = $('#InsuranceCompanyjson').text();) I get my list from Html page and passes to Jquery Autocomplete. I know on request object I will get term to get user input but how do I use that input and query that Json object as we do in the database using LIKE Keyword.

2
  • Are you looking for jQuery specifically? Commented Sep 27, 2017 at 13:53
  • yes, but If some other option available then I would like to know. Thank you Commented Sep 27, 2017 at 13:55

1 Answer 1

2

Assuming that you store properly formatted JSON in #InsuranceCompanyjson element as text, you need to parse it back to JS object before using in javascript:

var data = JSON.parse($('#InsuranceCompanyjson').text());
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I tried this and also verify my Json string but still, it's not working.
I tried to alert on var data =JSON.parse($('#InsuranceCompanyjson').text()); but after alert my function stop working and in alert I am getting right josn output,
It's " Invalid 'in' operand obj "
I solved that issue but still, I want to filter that option based on user input as I describe in my question using request.term I will get user input and then I want to filter my list based on that. To solve above issue we have to make one step more in your answer which is var Jstring = eval('(' + $('#InsuranceCompanyjson').text() + ')'); var data = JSON.parse(Jstring);
If you need to make eval step (which is bad idea) it means that you didn't provide proper JSON in the first place. Write JSON into #InsuranceCompanyjson and you will not need extra step. In any case instead of eval, use two JSON.parse.

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.