Using the JQuery autocomplete I have a function that produces an Array of friends that a user entered in the box.
This array is called selected_Id. However, our project has expanded and along with the ‘friends’ selection (<--the one that produces the selected_Id array)
we would like to add a ‘enemies’ function.
It is possible to rewrite that snippet of code to produce 2 separate arrays? If I keep them separate, it will call the getfriends function twice (the getfriends function produces an array of available friends that loads into the jQuery ui that the user gets to choose from).
here is what I have:
$(function() {
var available = new Array();
available = getFriends();
available = my_friends
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
available, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
//replaced value with label to continue showing the names selected
terms.push( ui.item.label);
//ui.item.valuethis is where my
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
$( "button" ).button();
$( "button" ).click(function() {
var selected_Text = $( "#tags" ).val();
var selected_Friends = selected_Text.split(",");
//friend name put in the autocomplete box
var friend_Name = "";
//selected id of the person in the array
var testCount = 0;
//i < j --> run through interation once
for(var i = 0, j=selected_Friends.length; i<j; i++ )
{
friend_Name = selected_Friends[i];
while (friend_Name[0] == " "){
friend_Name = friend_Name.substring(1,friend_Name.length);
}
for (var k = 0, l = my_friends.length; k<l; k++)
{
if (friend_Name == my_friends[k]["label"])
{
selected_Id.push(my_friends[k]["value"])
testCount++;
} else {
}
}
}
return false;
});
});
You could return an object of arrays: