-1

Good Afternoon,

Junior developer question...

I'm using jquery autocomplete in this tagging plugin:

$('#hh').tagEditor({
  duplicateTagClass:'bounce',
  initialTags:['Insert','tags','here'],
  placeholder: 'Insert tags here',
  autocomplete: {
    delay: 0,
    minChars: 1,
    position: {collision:'flipfit'},
    source:  ['a','b','c']}

I need to know how to insert in the local array the user's input. The user types 'a', autocomplete shows results but if the users types 'k' press enter i need that 'k' value to be saved as part of the autocomplete source.

Hope i explained myself clear enough. Thanks in advance.

1
  • The JavaScript code you have is either incomplete or invalid. Please elaborate. Commented Mar 11, 2016 at 14:51

1 Answer 1

0

please check https://jsfiddle.net/alanshearer/hfgz5e6e/4/ You can use onChange callback for tageditor widget to work out your issue:

HTML:

<input id="tags-editor" />

Javascript:

var source = ['a','b','c'];

var tagseditor = $('#tags-editor').tagEditor({
    duplicateTagClass:'bounce',
  initialTags:['Insert','tags','here'],
  placeholder: 'Insert tags here',
  autocomplete: {
    delay: 0,
    minChars: 1,
    position: {collision:'flipfit'},
    source: source
  },
  onChange: function(field, editor, tags) {
  var value = tags.reverse()[0];
  if ($.inArray(value, source) =='-1')
      source.push(value);
  }
});
3
  • Mother of God this works! Are you one of the plugin creators? can you explain me the logic behind why it works. Thanks a lot!! when i grow up want to be a good developer like you jejeje.
    – JjAA
    Commented Mar 11, 2016 at 16:12
  • haha... Unfortunately I'm not!! you're welcome!! Commented Mar 11, 2016 at 16:16
  • Then, about the logic: if you add an element, function onChange is called and it gives you back three parameters, In this case you need only the third one: tags, that represents the array of inserted elements - and obviously the last one inserted. You make a check if last element is contained in source array and if not add it to. Commented Mar 11, 2016 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.