Skip to content

can not go to next filed after open select2-bootstrap-theme #97

Description

@swadallail

I can't move to the next field when I open Select2. Also, the cursor doesn't automatically move to the search field.
Use the code to move between fields by pressing Enter.

this work with input and normal select and button but not working with select2

  $(document).on('keydown', 'input, select, button', function(e) {
    if (e.key === "Enter") {
      const fields = $('input, select, button').filter(':visible'); // Select all visible fields
      const currentIndex = fields.index(this); // Find the index of the currently focused field
      let nextIndex = currentIndex + 1; // Start looking at the next field

      while (nextIndex < fields.length) {
        const nextField = fields[nextIndex]; // Get the next field

        if (!nextField.readOnly) {
          const $next = $(nextField);

          if ($next.is('button') && $next.attr('id') === 'submit-form') {

            // window.alert("document.getElementById.submit();");

            var self = $(this);
            form = self.parents('form:eq(0)');
            form.submit();
            // return;
          } else {
            e.preventDefault();
          }

          if ($next.is('button') && $next.attr('id') === 'calc') {
            getCalculate();
          }

          nextField.focus();
          break;
        }

        nextIndex++;
      }
    }
  });

  
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions