0

I am adding content to a list dynamically and I want to be able to prevent adding duplicates. So before content is added I am selecting all the list, make an array and check that the data I am adding is not already contained in the list. However I get a unexpected identifier in my code. This is my code:

$(".autocomp_centers").autocomplete({
    var ids = new Array();
    $("#sister-centers-list > li").each(function(index, value){
        ids.push($(value).attr('class'));
        console.log($(value).attr('class'));
    });

    serviceUrl:'/suggest_centers',
        maxHeight:400,
        width:252,
        params: {country: $("#country").val() },
        minChars:2,
        onSelect: function(value, data){
            if ($.inArray(data, ids) == -1){
                alert("Sister center has already been added.");
            }else{
                $("#hidden-ids").append('<input type="hidden" name="center_ids[]" value="' + data +'" class="center-' + data + '"/>');
                $('#sister-centers-list').append('<li class="center-' + data + '">' + value + ' <a href="#sister-center" class="remove-center-before-save" id="' + data + '">Remove</a></li>');
            }               
            $("#sister-search").val("");
        }
});

When the person starts typing and this method is called, I get the list values and store them in an array. When they select a value, I check if the value is already in the array. This is the line that causes the error: var ids = new Array();

3
  • autocomplete expects an array of options. Your ids declaration adn the following .each should be moved out of the autocomplete block. Commented Jan 9, 2013 at 10:31
  • So how would you suggest I gather the contents of the list everytime the autocomplete is triggered? Thanks Commented Jan 9, 2013 at 10:34
  • I just pointed out what is wrong with your code. If this selection of Ids needs to run only once move it complete out of the autocomplete declaration into your document.ready Commented Jan 9, 2013 at 10:36

2 Answers 2

1

The autocomplete command (http://api.jqueryui.com/autocomplete/) requires an object as a parameter -- you cannot write the code directly between the object properties.

Try something like this instead:

var ids = new Array();
$("#sister-centers-list > li").each(function(index, value){
    ids.push($(value).attr('class'));
    console.log($(value).attr('class'));
});

$(".autocomp_centers").autocomplete({
    serviceUrl:'/suggest_centers',
    maxHeight:400,
    width:252,
    params: {country: $("#country").val() },
    minChars:2,
    onSelect: function(value, data){
        if ($.inArray(data, ids) == -1){
            alert("Sister center has already been added.");
        }else{
            $("#hidden-ids").append('<input type="hidden" name="center_ids[]" value="' + data +'" class="center-' + data + '"/>');
            $('#sister-centers-list').append('<li class="center-' + data + '">' + value + ' <a href="#sister-center" class="remove-center-before-save" id="' + data + '">Remove</a></li>');
        }               
        $("#sister-search").val("");
    }
});

Of course, depending on what your application does, you may have to make more changes. For example if you have multiple "autocomplete" things on the same page, you may want to use a separate array of IDs for each of them. Etc.

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to get the values in the onSelect function. Thanks for the response.
1

You've started an object literal when it looks like you intended to start a function.

{
    var ids = new Array();

You probably want:

function () {
    var ids = new Array();

5 Comments

Also, use literals instead of constructors. var ids = []
@Amberlamps — That's largely a stylistic choice, and doesn't impact the problem described in the question.
I never said that it impacts the problem in anyway. You already hinted him in the right direction. Yet it does not hurt to throw in some best practices.
Hi guys, I just need an array variable to store the values. Im using var ids = [] now and still the same problem
@Quentin -- seems like it's the other way around: autocomplete expects OBJECT, and the program fails because there is a CODE inside the object, which is syntactically wrong.

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.