Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow comma in bootstrap tags input filter?

I am using below code to submit tags using bootstrap tags input filter, I want to allow comma in tags. Please help.

$('.tagsinput').tagsinput({
        maxTags: 15,
        trimValue: true,
        confirmKeys: [13],
        allowDuplicates: false,
        onTagExists: function(item, $tag) {
            alert('Tag already exists')
            $tag.hide.fadeIn();
          }
    });
like image 484
TIGER Avatar asked May 09 '15 13:05

TIGER


1 Answers

Edit

Since the time of the original writing it looks like two options now exist for this: delimiter and delimiterRegex. So you should be able to do .tagsinput({ delimiter: '|' }) or .tagsinput({ delimiterRegex: /\s+/ }). The default is still ,.

Original post

There is no option for that, so you'd have to change the code for the plugin. Line 87 of bootstrap-tagsinput.js splits on the , character. You could change that to be a different character, like ; or |. If you wanted to make it more extensible you could add splitOn: ',' to defaultOptions, then and change line 87 to be var items = item.split(self.options.splitOn);. You can add self.options.splitOn && to the if statement above too, which will keep it from trying to split when there's nothing to split on.

So, the code changes will need to look like this:

// Line 4
var defaultOptions = {
    splitOn: ','
    // ...
};

// Line 86
if (self.options.splitOn && typeof item === "string" && this.$element[0].tagName === 'INPUT') {
  var items = item.split(self.options.splitOn);
  // ...
}

You'll want to keep using confirmKeys: [ 13 ], and you'll probably want to use a <select multiple></select> instead of an <input /> so that you get an array instead of a comma separated string when you do $(".tagsinput").val();

Here's an example.

like image 176
redbmk Avatar answered Nov 18 '22 13:11

redbmk