bootstrap-tagsinput form submited on press enter key

Mojnegar it picture Mojnegar it · Jun 22, 2016 · Viewed 10.4k times · Source

in bootstrap-tagsinput on press enter key for next tag form is submitted! what is the solution?

$("#inputID").tagsinput();

Answer

Shelby S picture Shelby S · Sep 20, 2016

First, you'll want to map 'Enter' to your tagsinput's confirmkeys option, then you'll need to call preventDefault() on your enter key to prevent the form from being submitted. In my solution, it only prevents submission by enter key while the user is focused in the tags input field.

To add a bit of redundancy, I also re-map the enter key to a comma in the input field, just to be sure.

  $('#tags-input').tagsinput({
    confirmKeys: [13, 188]
  });

  $('#tags-input input').on('keypress', function(e){
    if (e.keyCode == 13){
      e.keyCode = 188;
      e.preventDefault();
    };
  });

Just also an FYI, Enter key is mapped as 13, and comma is 188 in JS.