4

I am using JQuery Select2 component (see here: http://ivaynberg.github.io/select2/ ) but I have problem configuring it so it would serve dual purpose - as autosuggest for matching data but also as a text box for new entries. Can someone help me configure that component correctly, please?

Roman Kagan
  • 10,440
  • 26
  • 86
  • 126

2 Answers2

3

Instead of

$('#name').select2();

You can try to use this:

$('#name').select2({
    multiple: false,
    tags: true,
    createTag: function (tag) {
        return {id: tag.term, text: tag.term, tag: true};
    }
});
Tân
  • 1
  • 15
  • 56
  • 102
dzuffy
  • 31
  • 2
1

I believe you are looking for the Tagging option:

Select2 Tags documentation

It will allow you to set up your own data to match to but accepts custom inputs.

$("#e20").select2({               
    tags:["red", "green", "blue"],              
    tokenSeparators: [",", " "]
});
Kelz
  • 494
  • 4
  • 9
  • That will look really odd if you only want one item I'd suggest you use this instead: http://stackoverflow.com/questions/14577014/select2-dropdown-but-allow-new-values-by-user – user2019515 Jun 10 '14 at 00:53