I have this autoplete plugin from Andrew Whitaker - DEMO Lets say I have a string in a textarea
"@peterwateber wele"
I want it to output in a hidden tag as
"@[peterwateber] wele"
how should I do it? I'm not that good at Javascript...
I've tried looking at this code here from Hawkee
I have this autoplete plugin from Andrew Whitaker - DEMO Lets say I have a string in a textarea
"@peterwateber wele"
I want it to output in a hidden tag as
"@[peterwateber] wele"
how should I do it? I'm not that good at Javascript...
I've tried looking at this code here from Hawkee
Share Improve this question edited Jun 7, 2019 at 7:02 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 8, 2012 at 5:06 Peter WateberPeter Wateber 3,9485 gold badges22 silver badges27 bronze badges 10- Hiya, do you mean something like this let me know if this helps, I will set it as answer jsfiddle/LHNky/3 , have a nice one! cheers. or let me know if its something else will try to help you out! – Tats_innit Commented Apr 8, 2012 at 5:19
- 1 What do you mean by "I want it to output in a hidden tag as"? – codef0rmer Commented Apr 8, 2012 at 5:21
- @Tats_innit yes absolutely! but the problem is how do I get to output the selected suggestion and at the same time at the hidden tag let say: "@peterwateber hey!" in the textarea and in the hidden tag "@[peterwateber] hey!" – Peter Wateber Commented Apr 8, 2012 at 5:21
- @codef0rmer if the textarea's value is = "@peterwateber hello" then the hidden tag value would then be "@[peterwateber] hello" – Peter Wateber Commented Apr 8, 2012 at 5:22
- 1 Okies, I might have solution for you then, but just to double check you want drop down i.e. hidden value = @[foobar] yay and the selected value which appear in the texture as @foobar yay ?? if thats the case above jsfiddle does that or you want opposite? sorry if I sound thick but want to doble check before I answer :) CHeerios! – Tats_innit Commented Apr 8, 2012 at 5:25
2 Answers
Reset to default 4Hiya Working demo here: http://jsfiddle/67dxH/
Already had good discussion above like you said behavior is like: value of the hidden tag as = @[C#] and the textarea as @C#
Jope this is helpful man, let me know how it goes, cheers! :)
Jquery Code
function split(val) {
return val.split(/@\s*/);
}
function extractLast(term) {
return split(term).pop();
}
function getTags(term, callback) {
$.ajax({
url: "http://api.stackoverflow./1.1/tags",
data: {
filter: term,
pagesize: 5
},
type: "POST",
success: callback,
jsonp: "jsonp",
dataType: "jsonp"
});
}
$(document).ready(function() {
$("#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("autoplete").menu.active) {
event.preventDefault();
}
}).autoplete({
source: function(request, response) {
if (request.term.indexOf("@") >= 0) {
$("#loading").show();
getTags(extractLast(request.term), function(data) {
response($.map(data.tags, function(el) {
return {
value: el.name,
count: el.count
}
}));
$("#loading").hide();
});
}
},
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
ui.item.value = "@" + ui.item.value;
terms.push(ui.item.value);
// add placeholder to get the ma-and-space at the end
terms.push("");
this.value = terms.join("");
return false;
}
}).data("autoplete")._renderItem = function(ul, item) {
return $("<li>")
.data("item.autoplete", item)
.append("<a>@[" + item.label + "] <span class='count'>(" + item.count + ")</span></a>")
.appendTo(ul);
};
});
I wrote the original code mentioned here and have fixed the menu problem Peter was having:
http://www.hawkee./snippet/9391/