Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change focus when input field was equal with maxlength #482

Closed
rofifathurrohman opened this issue Sep 21, 2016 · 5 comments
Closed

Change focus when input field was equal with maxlength #482

rofifathurrohman opened this issue Sep 21, 2016 · 5 comments

Comments

@rofifathurrohman
Copy link

rofifathurrohman commented Sep 21, 2016

i will make input automaticly change focus to next input if input field was equal with maxlength, the input field has been integrated with Mottie Keyboard, it's possible to do that?

Here's DEMO : JSFIDDLE

If no use Virtual Keyboard it's easy use this code:

$("input").bind("input", function() {
        var $this = $(this);
        setTimeout(function() {
            if ( $this.val().length >= parseInt($this.attr("maxlength"),10) )
                $this.next("input").focus();
        },0);
    });

when i combine script above with Mottie Keyboard, it's doesn't work.

Sorry for my bad english, Thanks!

@Mottie
Copy link
Owner

Mottie commented Sep 21, 2016

Hi @rofifathurrohman!

If you look at the wiki page, you'll see this demo listed as "Enter a serial number in sequence".

Interestingly enough, I was just helping someone through email and we came up with this demo

$(function() {
  $("input").keyboard({
    position: {
      // position under center input
      of: $("input:eq(1)"),
      // move down 12px; not sure why it doesn't line up
      my: 'center top+12',
      at: 'center top'
    },
    enterNavigation: true,
    maxLength: 4,
    layout: 'num',
    autoAccept: true,
    usePreview: false,
    change: function(e, keyboard, el) {
      var len = keyboard.$el.hasClass("last") ? 4 : 3;
      if (keyboard.$el.val().length >= len) {
        // switchInput( goToNext, isAccepted );
        keyboard.switchInput(true, true);
      } else if (keyboard.$el.val() === "" && keyboard.last.key === "bksp") {
        // go to previous if user hits backspace on an empty input
        keyboard.switchInput(false, true);
      }
    }
  });
});

@rofifathurrohman
Copy link
Author

it's work, but if use alwaysOpen function doesn't work?

@Mottie
Copy link
Owner

Mottie commented Sep 23, 2016

When you set alwaysOpen the focus ends up on the last input.

I set the initialFocus option to false, but it didn't work for some reason. Adding focus to the first input after a delay also didn't work. I will need to investigate.

@Mottie Mottie added the Bug label Sep 23, 2016
@rofifathurrohman
Copy link
Author

okee, I'll wait for the sequel

@Mottie Mottie closed this as completed in 2ca4a9d Jan 13, 2017
@Mottie
Copy link
Owner

Mottie commented Jan 13, 2017

Hi @rofifathurrohman!

Sorry for taking so long! I finally fixed this problem!

Here is an updated demo: https://jsfiddle.net/Mottie/bwpc31nj/10/

Make sure to set:

  • initialFocus to false.
  • Add "keyboard-init-focus" class name to the first input. You can change this class. Use $.keyboard.css.initialFocus = "keyboard-init-focus"; to alter the class name.

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

No branches or pull requests

2 participants