Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit a tag with Bootstrap Tags Input plugin with space key?

I'm building a form with a field that is using the Bootstrap Tags Input plugin. This plugin inputs a tag once the user clicks the Enter key.

Can anyone help use the documentation of the plugin to input a tag once the user puts a "," (comma) or a space in the field?

Here is the documentation - http://timschlechter.github.io/bootstrap-tagsinput/examples/

2020 edit: heres the doco: https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/

like image 986
gespinha Avatar asked Jul 03 '14 13:07

gespinha


People also ask

How to make tags in Bootstrap?

Use a <select multiple /> as your input element for a tags input, to gain true multivalue support. Instead of a comma separated string, the values will be set in an array. Existing <option /> elements will automatically be set as tags. This makes it also possible to create tags containing a comma.

How do you use input tags?

The input tag is used within < form> element to declare input controls that allow users to input data. An input field can be of various types depending upon the attribute type. The Input tag is an empty element which only contains attributes. For defining labels for the input element, < label> can be used.

How do you create a tag input?

To create Tags Input in HTML CSS & JavaScript. First, you need to create three Files: HTML, CSS & JavaScript files. After creating these files just paste the following codes into your file. You can also download the source code files of this Tags Input project from the given download button.


2 Answers

You need to specify the confirmKeys parameter. From the documentation the default is to only have keycode for enter (13), but you can add the code for comma (44) and space (32) like this:

$('input').tagsinput({
    confirmKeys: [13, 32, 44]
});
like image 145
DavidG Avatar answered Nov 03 '22 01:11

DavidG


I couldn't get confirmKeys to work. No error, it just made no difference to operation.

My root problem was that enter was submitting the form instead of creating the tag.

This issue solved it for me:

https://github.com/bootstrap-tagsinput/bootstrap-tagsinput/issues/625

Code reproduced here:

$('.bootstrap-tagsinput input').keydown(function (event) {
    debugger;
    if ( event.which == 13) {
        $(this).blur();
        $(this).focus();
        return false;
    }
})

If the field recieves enter, it removes and returns focus.

return false stops the enter key event bubbling up to the form.

With regards to using space, you should be able to use event.which == 32 but that doesn't function for some reason.

EDIT:

After this I noticed that even when I removed the code, enter worked as required

Then I noticed the cancelConfirmKeysOnEmpty option. So I guess my original form just wasn't set up properly

like image 21
Nick.McDermaid Avatar answered Nov 03 '22 00:11

Nick.McDermaid