Ryan Hipkiss
Ryan Hipkiss

Reputation: 678

jQuery Select2 - add custom typed value on save

I have a page that allows the user to insert some data into my MySQL database. I have a field for "Times of Year", which in best case would be 'Spring', 'Summer', 'Autumn', 'Winter'.

However, I want the user to be able to insert a custom time if they so wish, while having the ability to still choose from the already populated options in the database column.

I want to use jQuery select2 to prepopulate the select dropdown with the distinct values from the database.

Is it possible to use this while allowing them to add a custom value if they wish? (I'd like to keep it all in one form field if possible). Or is there a better option to do this?

Select2: https://select2.github.io/examples.html#single

There is no code for this yet, I'm trying to work my best option before starting anything.

Upvotes: 0

Views: 345

Answers (1)

Khallister
Khallister

Reputation: 266

I think that adding custom values is possible in select2, just set up additional option "tags: true" in your select2 config while initializing select element

$('.select2').select2({
    tags: true
});

I'm not sure about it at the moment, but give it a try and let us know if it worked :)

EDIT:

.select2 is just an example, you may change it if you want. Just remember to set your <select> class according to your config

Upvotes: 0

Related Questions