Selectize.js: Enter key should submit form

Created on 20 Aug 2013  Â·  37Comments  Â·  Source: selectize/selectize.js

I'm not sure if I am missing an option, but it would be nice to be able to allow the enter key to submit the form, even when selectize has focus.

consensus on need enhancement

Most helpful comment

Hey! Here's my improved version, based on @fjsj 's code, which fixes 2 issues:

  • triggers submit event if Enter is pressed but no option is selected
  • triggers submit event after user presses DOWN (which activates the dropdown options) then Enter in order to select the item he wanted (after pressing some UP/DOWN to navigate to it).
Selectize.define('enter_key_submit', function (options) {
    var self = this;

    this.onKeyDown = (function (e) {
      var original = self.onKeyDown;

      return function (e) {
        // this.items.length MIGHT change after event propagation.
        // We need the initial value as well. See next comment.
        var initialSelection = this.items.length;
        original.apply(this, arguments);

        if (e.keyCode === 13
            // Necessary because we don't want this to be triggered when an option is selected with Enter after pressing DOWN key to trigger the dropdown options
            && initialSelection && initialSelection === this.items.length
            && this.$control_input.val() === '') {
          self.trigger('submit');
        }
      };
    })();
  });

// Include this in $('#yourSelector').selectize({
  plugins: ['enter_key_submit'],
  onInitialize: function () {
    this.on('submit', function () {
      this.$input.closest('form').submit();
    }, this);
  },

All 37 comments

+1
Is there any progress on this? Or any workaround I can catch enter keypress? Thanks.

:+1:

I did this by hooking into the onCreate and onItemAdd methods. From there I fire classic custom jquery events. Outside of the selectize initalization I listen on this event and trigger the form submit from there with classic $(#form).submit()..

A bit complicated but it works: http://www.svb24.com (the top search bar)

I am also doing an additional if-condition for redirecting to links via. window.location.href instead of submitting the form...

@mablae care to share your code? Or can we just find it in the source of that page?

@davidpanzarella
http://www.svb24.com/themes/svb_black/js/jquery.svb-find.js

The code needs some more love, I am still implementing features in search module and after that, I'll clean up that code...

I've managed to change the code and implement "onenterkeypress" callback and "enter_keypress" event.

https://github.com/zevnikrok/selectize.js/blob/master/src/selectize.js

The changes can be seen here. Unfortunately, I cannot make dist yet, since grunt and bower dislike each other.

@brianreavis any chance you will build this into the core as an option? I noticed you did a bunch of great updates today, but no mention of this. Thoughts?

@davidpanzarella I've synched my fork with v0.9.1. So if you need it, you can get it there. And now it's a full compile, with minimized version also available.

Oh, i was actually curious about that as i saw that come through yesterday. Do you have any examples of how to implement it?

Also, he hasn't pulled it in yet right? If so, I'd assume it'd be 9.2 after.

Thanks!

As easy as that:

jQuery('#select-id').selectize({
    onEnterKeypress: function(el) {
        jQuery('#form-id').submit();
    }
}

That's dope! :)

Hmm.. doesn't seem to work.

Here's the call:

// selectize - recipe search
$('#keywords').selectize({
create: true,
maxOptions: 2,
persist: false,
highlight: false,
hideSelected: true,
addPrecedence: true,
maxItems: null,
openOnFocus: false,
onEnterKeypress: function(el) {
$('#search-recipe').submit(); // the ID of the form
}
});

Here's the form elements: