Rebooting Vue.js within the function

Snippet of code for showing an input field and focusing on it when mouseover occurs:
Template:

<span @mouseover="hoverOn('givenName')">
    <input v-show="this.hovers['givenName']" ref="givenName" v-model="...

and

methods: { 
    hoverOn: function (name) { 
      this.hovers[name] = true 
      this.$refs[name].focus() 
    }, ... 

The input field becomes visible, but the focus is not applied immediately. When mousing over a second time, the focus works as expected. The function parameters are correct, and the hovers array is properly declared in the data section.
This issue may be due to Vue not setting the field visible before the focus method is called. Tried using this.$forceUpdate() between the first line and the focus lines, but it did not resolve the problem.

Answer №1

Indeed, you are correct - the issue with focus not working is due to the element not being rendered yet. To resolve this, simply place the focus call inside a nextTick callback. Vue will then ensure that the element has finished rendering before triggering the focus.

methods: {
  hoverOn: function (name) {
    this.hovers[name] = true;
    this.$nextTick(() => {
        this.$refs[ref].focus()
    })
},

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

No active Pinia was found when calling getActivePinia in Vue

I encountered an error message saying: Uncaught Error: [ ...

Converting a JSON object into a Vue v-for friendly structure

My goal is to display data in an html table using the Vue.js v-for directive. However, I'm encountering issues with building the table. I suspect that the data format is incorrect, and I may need to manipulate it to eliminate the object layer (index o ...

What steps can I take to avoid Cumulative Layout Shifts when deploying a Vue3 application?

My understanding is that Vue3 relocates the content of the mounted element to the virtual DOM. However, this results in the content momentarily disappearing and then reappearing when the app mounts, leading to noticeable Cumulative Layout Shift. Although ...

What is the best approach for adding a mark within a circle using React or vanilla JavaScript?

Has anyone come across an npm package that allows for marking a dot in a circle? I am looking to click on the mark and output the JSON object. https://i.sstatic.net/u8FTY.png Additionally, is there any option to achieve this with a simple onClick event? ...

I am experiencing issues with the POST method in my RESTAPI in node.js and it is not functioning

I recently started learning Node.js and Express.js with the goal of creating a basic API to retrieve data from a JSON file using the GET method, and to add a new user using the POST method. The GET method is functioning correctly, but I have encountered a ...

Sending a CSS class name to a component using Next.js

I am currently in the process of transitioning from a plain ReactJS project to NextJS and I have a question. One aspect that is confusing me is how to effectively utilize CSS within NextJS. The issue I am facing involves a Button component that receives ...

accessing information from webpage using hyperlink reference

This seems to be a rather straightforward issue, but unfortunately, I lack the necessary expertise to address it. Despite conducting thorough research, I have been unable to find a solution - mainly because I am uncertain about what specific terms or techn ...

The functions getFiles and getFolders will consistently retrieve a single file or folder each time they are

When attempting to fetch all files and folders from my Google Drive, the function .getFiles() is only returning one file, while .getFolders() is only returning a single folder. However, I can confirm that there are multiple folders and files in my drive. ...

Obtain the GMT date for the next day

Need help with code to get the current date and the current date + 1 day (in GMT). var now = new Date(); var newexp = (now + 3); var show = newexp.getGMTString(); alert(show); Goal is to set a cookie to expire in 1 day. function SetCookie(name, value, ...

Retrieving an ID from one controller and incorporating it into another controller's API request

Here is my original module: angular.module('ngApp', []) .factory('authInterceptor', authInterceptor) .constant('API', 'http://myserver.com/ecar/api') .controller('task', taskData) function taskData($scope ...

Error message: When the mouse hovers over, display the chart(.js) results in TypeError: t is

I encountered a persistent error that I just can't seem to resolve. My goal is to showcase a chart using Chart.js when the mouse hovers over the canvas. However, upon hovering over the canvas, I keep getting a TypeError: t is null error. Error: { ...

What is the best way to create a filter function that continues past the first false value?

Looking for a solution to successfully filter an array of objects by comparing it to another array through looping over its values. The issue arises when the filter function stops at the first false, preventing the full comparison. Any suggestions on how ...

Guide on how to fetch Laravel's CSRF token with a separate Vue frontend

Is it possible to transfer the Laravel csrf token to Vue when the backend and frontend are located in separate directories and subdomains? I am working on an application that requires a distinct separation between the backend and frontend for organizationa ...

Unable to perform a GET request to an API with custom headers

I am attempting to send a GET request to an API, but I am encountering an error: Failed to fetch. What could be causing this issue? const getData = () => { fetch("https://test-docs.stores.kg/api/categories", { method: "GET", he ...

How come jQuery won't let me activate the 'change' event on a radio button?

Click the link below to access the page: Below are the original javascript codes that are functioning correctly: $(document).ready(function(){ $(".open_gene").on('change', function(event) { $('#Gene_field').show(); }); ...

There are two different ways to set a hyperlink in HTML. One method is by using the href attribute, where you provide the URL inside the quotation marks. The other

While browsing, I stumbled upon this interesting piece of JavaScript code: onClick="self.location.href='http://stackoverflow.com/'" I incorporated this code into my website, and it seems to have the same effect as using the href attribute. Sin ...

Ways to display the modal once the user initiates the action

Is there a way to delay loading my modal HTML codes until after the user clicks a button, rather than having them load automatically with the template? HTML <!-- Template Codes--> <button data-toggle="modal" data-target="#modal-content" type="bu ...

Error in Express Session: Property 'signin' is not found in type 'Session & Partial<SessionData>'. Code: 2339

I received the following Error Code: Property 'signin' does not exist on type 'Session & Partial<SessionData>'. (2339) About My Application src/index.ts import "reflect-metadata"; import express = require("expr ...

Leveraging SystemJS/jspm for loading asynchronous, es5 modules in a production environment

I am looking for a way to dynamically load dependencies using System.import(), without the need to transpile ES6 to ES5 at production runtime. My goal is to have these modules transpiled into separate ES5 modules that are only fetched when necessary, rathe ...

The slider class in the div is not showing the image, but it is visible in the elements

Is the sequence of loading .css and .js files in the head section important for displaying images with the .slider class on MaterializeCSS? <!DOCTYPE html> <html> <head> <!--Import Google Icon Font--> <link href="htt ...