I have been using Andrew Whitaker’s code at jsfiddle.net/5xbhY. I was hoping someone could help me make it work for both @user and #tags (they come from different external db’s but I should be able to write that part what I am having trouble with is detecting the hash).
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
var startTyping = "Start typing...";
function split(val) {
return val.split(/@/);
}
function extractLast(term) {
return split(term).pop();
}
$("#tags")
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function(request, response) {
var term = request.term,
results = [];
if (term.indexOf("@") >= 0) {
term = extractLast(request.term);
if (term.length > 0) {
results = $.ui.autocomplete.filter(
availableTags, term);
} else {
results = [startTyping];
}
}
response(results);
},
focus: function() {
return false;
},
select: function(event, ui) {
if (ui.item.value !== startTyping) {
var terms = this.value.split(' ');
terms.pop();
terms.push("@" + ui.item.value);
this.value = terms.join(" ");
}
return false;
}
});
Do you mean something like this: http://jsfiddle.net/BfAtM/181/
Oh, I noticed a bit late I’m responding to a 3 months old question.