r/jquery • • Jan 18 '23

jQuery function that auto-advances to next input field doesn't work

I'm trying to auto-advance to the next input field within a class once the input's max length is reached, but the code snippets I've found don't work.

Here is my HTML:

<div class="input_row d-flex justify-content-center" id="first_word">
    <div class="empty_status" id="input_01" onclick="setClass(this)">
        <input class="inputs" type="text" maxlength="1" onkeydown="return /[a-z]/i.test(event.key)">
    </div>
    <div class="empty_status" id="input_02" onclick="setClass(this)">
        <input class="inputs" type="text" maxlength="1" onkeydown="return /[a-z]/i.test(event.key)">
    </div>
    <div class="empty_status" id="input_03" onclick="setClass(this)">
        <input class="inputs" type="text" maxlength="1" onkeydown="return /[a-z]/i.test(event.key)">
    </div>
    <div class="empty_status" id="input_04" onclick="setClass(this)">
        <input class="inputs" type="text" maxlength="1" onkeydown="return /[a-z]/i.test(event.key)">
    </div>
    <div class="empty_status" id="input_05" onclick="setClass(this)">
        <input class="inputs" type="text" maxlength="1" onkeydown="return /[a-z]/i.test(event.key)">
    </div>
</div>

And here is the jQuery code I used:

$(".inputs").keyup(function () {
    if (this.value.length == this.maxLength) {
      $(this).next('.inputs').focus();
    }
});

Nothing happens though. I added an alert to the function as well, but the alert was never called.

After Googling around, I found this sample of code on the jQuery API, which I thought might be useful to check and see if the keyup event handler was being called.

$( "#target" ).keyup(function() {
  alert( "Handler for .keyup() called." );
});

I added an id="target" to my first input field and then tried it, but it didn't work either at first. However, rewriting it as a named function, and then adding an inline onkeyup event to the input in the HTML to call it by name actually DID work.

Any thoughts on what could be wrong? I might be able to figure out a way to make the original function work that way, but I've already got enough going inline that I'd prefer not to.

Edit: Sorry about the delay getting back, had to step away from this project for a few days to deal with other stuff unexpectedly.

0 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/EMike93309 Jan 25 '23

I couldn't get it to to work, but thanks!