What is causing some keyup values to not stay in the input field even when they are passed as parameters?

Currently facing an unusual issue related to processing time. The problem seems to be with a PIN input that consists of 4 inputs. You can observe the behavior in action on this stackblitz code snippet I have set up: https://stackblitz.com/edit/vue-fezgmd?file=src%2Fcomponents%2FHelloWorld.vue

Focus on these two functions: nextInput and handleKeyUp. I have maintained the remaining code to ensure seamless functionality without causing crashes.

Assuming everything is working correctly ⇒ handleKeyUp will trigger nextInput and pass a parameter called key, representing the pressed key. Here's how nextInput function operates:

    const nextInput = (key: string): void => {
      console.log(key)
      if (
        focusOn.value !== null &&
        focusOn.value < props.length - 1
      ) {
        code[focusOn.value] = key
        inputs[focusOn.value + 1].focus()
        }

focusOn.value => index

code => reactive object[Array] bind to the v-model input

The objective here is to assign the key value to the respective input field. However, there are instances (particularly when typing rapidly) where the console.log(key) displays the pressed key, but the input field does not retain that value, as shown here: https://i.sstatic.net/7tjBl.png

You can see that "5" is logged in the console, indicating the key pressed, yet the last input field does not display the assigned value.

I have attempted various conditional statements without success, struggling to identify the root cause. Any assistance or suggestions would be greatly appreciated!

Answer №1

The issue lies within the nextInput() function.

if (focusOn.value !== null && focusOn.value < props.length - 1)

When the last key is pressed, focusOn.value equals 3 and props.length - 1 also equals 3. As a result, the condition inside the if statement is not met, causing the assignment to the code array to be skipped:

code[focusOn.value] = key;

Since the purpose of the if statement is only to determine whether to focus on the next input, moving this line above the if statement is a safe option:

const nextInput = (key: string): void => {
      code[focusOn.value] = key;
      if (focusOn.value !== null && focusOn.value < props.length - 1) {
        inputs[focusOn.value + 1].focus();
      }
    };

This issue occurs when typing quickly because multiple keys are being pressed simultaneously (such as rolling fingers across the keys), resulting in multiple keyups without corresponding keydown events. Typing slowly allows the last input's v-model to correctly handle the value assignment.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a Webpack plugin available that can analyze the usage of a function exported in a static

Just to clarify, I am presenting this theoretical scenario on purpose, as it reflects a genuine issue that I need to solve and am uncertain if it's feasible. Imagine I have a JavaScript package named road-fetcher, containing a function called find wh ...

Bootstrap5: Left-aligned Navigation Bar Pills and Right-aligned Text

I am trying to align all my navigation pills to the left, and then add a single text element that stays at the end of the navbar even when the page is resized. Navbar Image My attempt involved adding a div so that the navbar pills would take up 50% width ...

Develop a unique Kotlin/JS WebComponent featuring custom content

I am trying to create a custom tag using Kotlin that includes default content. While the example I found works well, I am having trouble adding default content (such as an input element) inside the custom tag. After attempting various approaches, I have o ...

Ways to identify whether the ajax error is due to Access-Control-Allow-Origin restriction or if the file is genuinely absent

My question pertains to customizing error messages for Access-Control-Allow-Origin issues and outdated URLs. I want to display specific messages to the user based on different types of errors that may occur. Currently, my code looks like this: $.ajax( { ...

Guide on clearing filters in Angular 4

I'm facing an issue where I have implemented multiple filters but resetting them is not working as expected. showOnlyMyRequest(){ this.requests = this.requests.filter(request => request.requestedBy === 'John Doe'); } showAllReques ...

React Native: Unexpected Challenge in State Updates

Below is the code I am currently using: makeRemoteRequest = () => { let items = []; models.forEach(element => { //models contains an array of different models this.pushReports(element, items); }); console.log("The item ar ...

What is the method for swapping out the <button> element with the enter key?

I have a webpage where users can post status updates, and other users can comment on these statuses by typing in the textbox and clicking the 'Reply' button. However, I would prefer to remove the button so users can simply press the enter key to ...

What is the best way to animate various sprites using distinct buttons in HTML and CSS?

I've been experimenting with animating a sprite by using different onClick button triggers. I encountered an issue where only one of the buttons works correctly in the fiddle. However, in my local file version, the other buttons work but only once and ...

Similar to session_start() and $_SESSION in Node.js/Express

I'm on a quest to discover the equivalent of session_start() and $_SESSION in Node.js/Express so I can store the current user's id in the session. Most tutorials and videos recommend using express-session, but I've come across a warning: ...

Manipulating browser geolocation with JavaScript

I am currently working on automating tests for a web application using Selenium. The application relies on obtaining the user's current location information through the HTML5 Geolocation API. However, for testing purposes, I need to simulate a fake lo ...

Adding content before an element in an Angular directive

Shown below is the HTML code: <div class="row"> <div ng-if="isLoadingApprovalUrl" class="col-xs-12"> <div class="text-center"><span class="fa fa-spinner fa-pulse fa-5x fa-fw margin-bottom"></span></div> </div ...

Compiling TypeScript into JavaScript with AngularJS 2.0

Exploring the capabilities of AngularJS 2.0 in my own version of Reddit, I've put together a script called app.ts ///<reference path="typings/angular2/angular2.d.ts" /> import { Component, View, bootstrap, } from "angular2/angular2 ...

The Lightgallery plugin is creating three duplicates of the slides

I am currently facing an issue with a gallery that loads images from an API and displays them using the lightgallery plugin. Upon implementing the lightbox in the correct location (view question here), I discovered that the plugin is generating three slid ...

Is there a way to determine if any word within a given text is present in an array?

Imagine you have the following full text: var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy" And a given array like: ["Afghanistan", "Italy", "Albania", "United Arab Emirates"] How can we verify if the exact word Italy within that ent ...

Why is it that I am unable to utilize the post data stored in $var within my PHP document when making an Ajax call?

Hey there! I've got this function that makes an ajax call. But, for some reason, the $value I'm passing isn't defined in my showuser.php file. Can you help me figure out why? function showUser2(value) { var xhr = new XMLHttp ...

Tips for running a dry default with Angular CLI

Query: Can dry-run be set as the default in a configuration? Purpose: Enabling dry-run by default simplifies the learning process by minimizing clean-up tasks if the command is not correct. This can encourage users to always perform a test run before exec ...

How can I implement Google Analytics Event tracking for all <audio> tags using Javascript?

I am looking to implement a Google Analytics Event for all audio tags on my website. Currently, I have a script that targets audio tags with a specific class: <script> /* a test script */ $(function() { // Execute function when any element with t ...

What is the process for loading data with no input provided?

I have come across a situation where my HTML table is populated with various account numbers. https://i.sstatic.net/qJc2E.png When I input the account number 979545130406, the filter works perfectly fine. https://i.sstatic.net/Y4Rwk.png However, the is ...

Is there a universal browser variable where I can attach a listener to capture any errors that occur?

Currently, I am utilizing Selenium to navigate an AngularJS website and am keen on generating a comprehensive catalog of all errors that are thrown (my main focus is on lex errors). AngularJS provides access to $exceptionHandler for altering exception hand ...

What is the significance of h being undefined?

I have successfully set up a new Vue project using vue create and added Storybook with no issues. However, after installing storybook-addon-designs and following the instructions in the readme to add it to my story, I encountered an error in my console sa ...