How can you utilize a computed property in a Vue component to select all the text within it?

When using $event.target.select(), I usually can select all the text. However, in this scenario, it seems to be selecting everything and then replacing the selection with the computed property. How can I select all after the computed property has finished?

Vue.component('my-component', {
  template: `
<div>
My Component
<input type="text" v-model="displayValue" @blur='isInputActive = false' @focus='isInputActive = true;$event.target.select()'></input>
</div>
`,
  props:['value'],
    data() {
        return {
            isInputActive: false
        };
    },
    computed: {
        displayValue: {
            get: function() {            
                return (this.isInputActive) ? this.value : this.value.toUpperCase();
            },
            set: function(val) {
              this.$emit('input', val);
            },
        }
    },
})

new Vue({
  el: '#app',
  data() {
        return {
            test: "Test"
        };
    },  
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <my-component v-model="test"></my-component>
</div>

Answer №1

Utilize the $nextTick method to execute the callback function once the computed property is completed.

@focus='isInputActive = true; $nextTick(() => $event.target.select())'

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

The function 'toBlob' on 'HTMLCanvasElement' cannot be executed in react-image-crop because tainted canvases are not allowed to be exported

Currently, I am utilizing the react-image-crop npm package for image cropping purposes. Everything works perfectly when I pass a local image as props to the module. However, an issue arises when I try to pass a URL of an image fetched from the backend - th ...

Saving URLSearchParams to a file using javascript

I am currently implementing jstree and I need to be able to click on a file within the tree structure in order to display a PDF file. Below is the relevant code snippet: $(function () { $('#tree').jstree({ 'core' : { ...

What is the best way to prevent the collapse v-b-toggle event from being triggered by an

I am currently using a v-b-toggle to toggle another div on and off. However, within the div where the v-b-toggle is located, there is an input checkbox. The issue that I am facing is that when I click on this checkbox, the toggle functionality is activa ...

Avoiding the issue of multiple submissions in Ajax forms

My website's contact form sometimes experiences a delay in sending submissions. When users, in their impatience, click the submit button multiple times, it results in the form being sent repeatedly to the host. To address this issue, I attempted to ...

Trouble with fetching data in Backbone

I'm facing an issue where the Backbone/Marionette Controller and Collection are not fetching properly. define(["jquery", "backbone","models/Poi"], function($, Backbone, Poi) { // Creating a new instance of Backbone Poi class object ...

Comparison of Grant and Passport.js: Which One to Choose?

Are you unsure about the distinctions between Grant and Passport.js? How do you decide when to utilize Grant over passport.js, and vice versa? If your goal is to create a social media platform that tracks user activities and posts them on a news feed, whi ...

What could be causing React to throw an error?

Check out this React Component: GeneralInformation = React.createClass({ totalCaptialRaisedPanel: function() { var offeringInfo = this.props.company.data.offerings.data[0]; var percentageComplete = (offeringInfo.capital_raised / offer ...

Is the Three.JS camera update causing issues with the shader?

Attempting to replicate the Bubble shader from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Bubble.html, but encountering issues due to outdated code. The example should refract whatever is behind it, like this: https://i.sstatic ...

Utilizing JQuery to make Google listings easily findable

Implementing Google Places for a location text box has been successful. However, I am now looking to create a class-based implementation so that I can use it with multiple places effortlessly. Is it possible to achieve this using JQuery? <script type ...

Internet Explorer 10 not triggering the 'input' event when selecting an option from the datalist

Within this particular scenario, there is an input field paired with a corresponding datalist element. My aim is to develop JavaScript code that actively listens for when a user chooses an item from the list. Most resources suggest utilizing the "input" ev ...

How can I make Vuejs update v-model when the text input is changed using Jquery?

I am currently working with 4 inputs that have a timeselector popup. The jquery timepicker plugin I am using does not seem to provide an obvious way to manipulate an input through a callback function. Interestingly, Vuejs does not detect changes made to th ...

transform pixel coordinates to latitude and longitude dimensions

Seeking clarification on how the geo referencing process functions for images. Is there a method to accurately extract latitude and longitude information from this specific line of code? imageBounds = [map.unproject([0, 0], 20), map.unproject([1716,1178], ...

Create a new object in Three.js every x seconds and continuously move each object forward in the Z-axis direction

I am currently developing a Three.js endless runner game where the player controls a character dodging cars on a moving road. At this early stage of development, my main challenge is to make the hero character appear to be moving forward while creating the ...

Unable to successfully display AJAX data on success

After successfully running my GradeCalc function in a MVC C# context with the grade parameter, I am facing an issue where the data is not displaying and the JavaScript alert pop up shows up blank. Can you assist me with this problem? $("#test").o ...

Error encountered in TypeScript's Map class

When working with TypeScript, I keep encountering an error message that reads "cannot find name Map." var myMap = new Map(); var keyString = "a string", keyObj = {}, keyFunc = function () {}; // assigning values to keys myMap.set(keyString, "val ...

Tips for redirecting to a 404 page when encountering invalid data in getStaticProps

For my Next.js application, I need to retrieve all articles written by a specific author. The author's ID is obtained from the request parameters. I have already pre-generated some authors using getStaticPaths. Within the getStaticPaths function, I h ...

Measuring Time Passed with JavaScript

I've been working on a small piece of code that needs to achieve three tasks: User should input their birthday User must be able to view the current date When the user clicks a button, they should see the time elapsed between their birthday and the ...

What is the best way to display an image/jpeg blob retrieved from an API call on screen using NextJS?

When using Next.js, I make an API call like this: const response = await fetch('/api/generateimageHG2'); This triggers the following function: import { HfInference } from "@huggingface/inference"; export default async function genera ...

I'm encountering an issue where Bulma is taking precedence over the CSS of my Vue3

In my Vue CLI 3 project, I'm utilizing Bulma and have included the import in the 'index.js' file like so: import { createApp } from 'vue' import App from './App.vue' import router from './router' require('@ ...

Dynamic Templating in Vue.js using URI

I am currently working on implementing dynamic Uri templating in Vue.js Here is the data: new Vue({ el: '#app', data: { "navigation": [ { "url": "https://domainxxx.org/{param1}", "param1": "John", ...