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();