i have a text box with auto complete feature. when you start typing country name in text box drop down will apeear automatically by matching character. selected country name will appear to the next div automatically and removed from available list. in contrust you can remove country from selected list which will return back country name in drop down again. let me dive a bit technically.
i have used two array variables availableAllTags and availableTags which assign same value. in whole program i have not change availableAllTags values anywhere. but surprisingly i found every time when i select a country from drop down it removes from availableTags(perfect) and availableAllTags(surprising). but i need to become fixed availableAllTags array values all over my program.
http://jsfiddle.net/aminulsumon/w73yr/11/
var availableAllTags = availableTags; //original array which data not changed anywhere
var selected_item_ids=new Array();
var country;
var i;
$(function() {
$( "#country_name" ).autocomplete({
source: availableTags,
autoFocus: true,
select: function( event, ui ) {
country=ui.item.value; //$('#target').text( ui.item.value );
$('#div_selected_country').html($('#div_selected_country').html()+"<span>"+country+
" <a href='?delete="+country+"' class='del_country'>X</a></span>"+"\n ");
//Add country id in selected_items array.
alert(availableAllTags[0]);
selected_item_ids.push(availableAllTags.indexOf(country));
//Remove country name from availableTags array.
i = availableAllTags.indexOf(country);
availableTags.splice(i, 1); //alert(availableTags[0]);
if ( ui.item ){
$(this).val('');
return false;
}
}
});
});
$(document).on('click','a.del_country', function(e) {
e.preventDefault();/* prevent browser opening href url*/
//Select country name and delete from availableTags array
var href_val = $(this).attr('href');
var item_name = href_val.split('delete=')[1]; //alert(item_name);
selected_item_ids.splice(selected_item_ids.indexOf(availableAllTags.indexOf(item_name)), 1);
/* remove parent span*/
$(this).parent().remove();
return false;
});
$(document).on('click','#show_selected_item_ids', function(e) {
var all_ids;
all_ids="";
$.each(selected_item_ids, function(index,value) {
all_ids = all_ids+ selected_item_ids[index]+ ",";
});
all_ids = all_ids.substring(0,all_ids.length - 1); // remove last ";" added
$("#div_selected_ids").html("ids: "+all_ids);
});
$(document).on('click','#btnSelectAll', function(e) {
selected_item_ids = [];
$('#div_selected_country').html("");
$.each(availableTags, function(index,value) {
selected_item_ids.push(availableAllTags.indexOf(value));
$('#div_selected_country').html($('#div_selected_country').html()+"<span>"+value+
" <a href='?delete="+country+"' class='del_country'>X</a></span>"+"\n ");
});
});
$(document).on('click','#btnRemoveAll', function(e) {
/*$.each(selected_item_ids, function(index,value) {
availableTags.push(availableAllTags.indexOf(value));
selected_item_ids.splice(selected_item_ids.indexOf(availableAllTags.indexOf(value)), 1);
});*/
selected_item_ids = [];
availableTags = availableAllTags;
$('#div_selected_country').html("");
$('#div_selected_ids').html("");
});
});