bootstrap-tagsinput form submited on press enter key
Asked Answered
A

6

9

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

$("#inputID").tagsinput();
Among answered 22/6, 2016 at 16:31 Comment(3)
I am facing the same issue ... have you found any solution.Draghound
which backend framework you use ? share sample code ??Veronikaveronike
Possible duplicate of how prevent submit on press enter in twitter bootstrap typeahead pluginAbut
P
13

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.

Probe answered 20/9, 2016 at 14:32 Comment(1)
This has been really helpful. I'd like to add an update. I think comma keycode changed to 44 now.Teresitateressa
B
6

Try this:

$('.bootstrap-tagsinput input').keydown(function( event ) {
    if ( event.which == 13 ) {
        $(this).blur();
        $(this).focus();
        return false;
    }
})
Baikal answered 20/3, 2019 at 10:17 Comment(0)
I
5

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 !!
}
Ingmar answered 24/5, 2018 at 12:33 Comment(2)
If the source is piece of sh*, I'd like to fix it. Have a beautiful day Jeff.Flashcube
Agreed 100%. The usual process is suggesting a fix and submit the fix with a PR though instead of editing the source directly.Seignior
V
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 .

Veronikaveronike answered 25/8, 2016 at 10:32 Comment(1)
The prevents the item from been added to the input in the first placeHouseline
C
1

This work for me

$('.bootstrap-tagsinput input[type=text]').on('keydown', function (e) {
   if ( event.which == 13 ) {
           $(this).blur();
           $(this).focus();
           return false;
      }
 });
Catatonia answered 7/7, 2020 at 14:58 Comment(0)
G
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();
}
Guidance answered 17/7, 2020 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.