been following the documentation from the UI site, and looked at a number of tutorials and got my code sort of working, it ajax's in the list of terms I want, and you can select one and it places the value and then ", ". However, afterwards it doesn't prompt again for a second value.
Also, when I enter a partial and say 10 entries pop up, tabbing just goes to the next form field, not sure what's wrong with my .bind, perhaps a setting? If anyone wouldn't mind taking a look, it'd be much appreciated!
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
jQuery('#tagSearch')
.bind('keydown', function(event) {
if (event.keyCode === jQuery.ui.keyCode.TAB &&
jQuery(this).data('autocomplete').menu.active) {
event.preventDefault();
}
})
.autocomplete({
autoFocus: true,
source: function(request, add) {
console.log('source');
jQuery.getJSON('/get-tags-like.php?term_start=' + request.term, function(response) {
var possibleTerms = [];
jQuery.each(response, function(i, val) {
possibleTerms.push(val.name + ' ' + '(' + val.count + ')');
});
add(jQuery.map(possibleTerms, function(item) {
console.log(possibleTerms);
return item;
}));
});
},
focus: function() {
console.log('focus');
return false;
},
select: function(event, ui) {
console.log('select');
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push('');
this.value = terms.join(',');
// At this point, example:
// Sony (5)
var currentVal = this.value;
// Sony (5) - Gets inserted as "Sony"
currentVal = currentVal.replace(/\s\(.\)/, '');
this.value = currentVal;
return false;
},
minLength: 2
});