9

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

$("#inputID").tagsinput();
rrauenza
  • 5,161
  • 4
  • 27
  • 45
Mojnegar it
  • 93
  • 1
  • 1
  • 4
  • I am facing the same issue ... have you found any solution. – Aman Jul 21 '16 at 15:09
  • which backend framework you use ? share sample code ?? – Noni Aug 25 '16 at 10:25
  • Possible duplicate of [how prevent submit on press enter in twitter bootstrap typeahead plugin](http://stackoverflow.com/questions/13551668/how-prevent-submit-on-press-enter-in-twitter-bootstrap-typeahead-plugin) – BeliG Oct 28 '16 at 14:12

6 Answers6

10

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.

wallacer
  • 11,227
  • 3
  • 22
  • 44
Shelby S
  • 330
  • 2
  • 17
5

Try this:

$('.bootstrap-tagsinput input').keydown(function( event ) {
    if ( event.which == 13 ) {
        $(this).blur();
        $(this).focus();
        return false;
    }
})
bharat
  • 1,336
  • 1
  • 16
  • 29
3

Go to tagsinput.js

Find line:

self.$container.on('keypress', 'input', $.proxy(function(event) {

Before the end of this function add following:

if (event.which == 13) {
    return false; // dont submit form !!
}
Martin Zvarík
  • 1,232
  • 13
  • 23
1

Enter key is register for postback form . that's why a case has been happened that when you hit 'enter' key it trigger the 'postback' event so the form goes to submitted . solution : on page load reserve the enter key for tag input :

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))

if using asp.net mvc framework this is a simple example .

Noni
  • 377
  • 2
  • 14
0

This work for me

$('.bootstrap-tagsinput input[type=text]').on('keydown', function (e) {
   if ( event.which == 13 ) {
           $(this).blur();
           $(this).focus();
           return false;
      }
 });
msayubi76
  • 396
  • 2
  • 10
0

The cancelConfirmKeysOnEmpty option is set to true by default, but when set to false calls .preventDefault on the tag delimeter keydown event. Set it to false.

This solution also fixes the issue of a "carried comma".

cancelConfirmKeysOnEmpty: false,

// If the field is empty, let the event triggered fire as usual
if (self.options.cancelConfirmKeysOnEmpty === false) {
  event.preventDefault();
}
Tumelo Mapheto
  • 141
  • 1
  • 5