How can Vue be used to dynamically change the input type on focus?

What Vue method do you recommend for changing an input element's type on focus?

e.g. onfocus="this.type = 'date'"

I am specifically looking to switch the input type from text to date in order to utilize the placeholder property.

My Solution:

<template>
    <input
        type="text"
        placeholder="Birthday"
        value="foo"
        @focus="setType('date')"
        @blur="setType('text')"
    />
</template>
<script>
    ...
    export default defineComponent({
        setup(){
            const el = ref<HTMLInputElement>()
            const setType = (x: string) => el.type = x

            return {el, setType}
        }
    })
</script>

Error Message:

Property 'type' does not exist on type 'Ref<HTMLInputElement | undefined>'

Answer №1

Introduce a new property named elType and connect it to the type attribute :

<template>
    <input
        :type="elType"
        placeholder="Birthday"
        value="foo"
        @focus="setType('date')"
        @blur="setType('text')"
    />
</template>
<script>
    ...
    export default defineComponent({
        setup(){
            const el = ref<HTMLInputElement>()
           const elType=ref('text')
            const setType = (x: string) => elType.value = x

            return {el, setType, elType}
        }
    })
</script>

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

Webpage video stalling due to buffering

Currently, I am developing personalized video controls and have integrated a @progress event to monitor the video buffering progress and adjust the width of a progress bar div: <video @progress="videoBuffer($event)"> videoBuffer(e) { if ...

How can I customize the font size of a label within a React Material UI Stepper?

Is it possible to modify the font size of stepper labels in React Material UI? If so, how can I accomplish this? function getSteps() { return [ "OPTION 1", "OPTION 2", "OPTION 3" "OPTION 4& ...

Tailored production method based on specific criteria

One of my challenges is to create a versatile factory method using typescript. The main objective is to initialize a class based on its name using generics, instead of employing if/else or switch statements. I am aiming for similar functionality as this ...

Is it acceptable to compare a boolean with a string?

In my code, I have a variable called isRefreshed which is initially declared like this: var isRefreshed = ''; Sometimes, in certain scenarios, isRefreshed can be assigned a boolean value, for example: isRefreshed = false; Now, there is an if ...

Disable or set input text to read-only in Internet Explorer 9 and earlier versions using JavaScript or jQuery

Can anyone offer any suggestions on how to accomplish this task? I attempted using JavaScript with the code below, but it did not yield the desired results. element.readOnly="true"; element.readonly="readonly"; element.disabled="disabled"; I also tried ...

stop the page from refreshing when clicking on the map area

http://jsbin.com/iTeNiJIt/1/edit?output I currently have multiple map areas on my webpage. When a user clicks on any of these areas, I want to create an animation without refreshing the page. However, I am facing an issue where the page refreshes every ti ...

Having trouble with NextAuth's Google provider on Safari?

I've encountered an issue where the Google provider works perfectly on Chrome and other browsers, but fails to work on Safari. Despite going through the documentation thoroughly, I couldn't find any relevant information to resolve this. This is ...

What is the best location to place Sentry's setUser function in a Next.js application?

I have been working on integrating user data into Sentry's scope globally so that user information is passed to it every time an error or event occurs. My application is developed in Next.js, and I followed the configuration mentioned in Sentry' ...

Having trouble setting up Node.js on a Mac computer

Upon completing the regular installation on Mac, I am unable to find Node in the terminal. Even after defining the PATH with the line: export PATH=/usr/local/bin: $PATH, it still does not locate Node or npm in the terminal. ...

Utilize the v-if directive with nested object properties for dynamic conditional rendering

I need to verify if the item object has a property called 'url' set. If it's not present, I would like to display a placeholder image. Here is an example of what I want to achieve: <img v-if="item.relationships.main ...

Inversify's Http Context consistently remains void of any content

Hello there, I'm in need of assistance with building an API using inversify and inversify-express-utils. Everything seems to be working fine with my controllers and the API so far, except for one issue. When trying to access the httpContext property i ...

JQuery failing to trigger AJAX function

I'm struggling with what seems like a simple issue and it's driving me crazy. The problem lies in this straightforward section of the website. I have a .js file that uses ajax to save a post into the database when the submit button is clicked. A ...

Variable missing in the ExpressJs view

Hey there! I'm new to Nodejs and currently experimenting with it. I've been trying to convert some of my basic Python codes to JavaScript. In one of my projects, I am sending a get request to the YouTube API and receiving 50 results in JSON forma ...

Strategies for dynamically incorporating customized text with unique ID attributes using jQuery while ensuring accuracy

I have a variety of divs structured like this: <div class="textwidget"><div> <div class="textwidget"><div> <div class="textwidget"><div> Here is the desired output for the divs: <div class="textwidget" id="widget-1 ...

SignalR gets stuck on the 'Initiating start request' screen, halting all progress

SignalR has been causing some strange behavior for me lately. After doing some refactoring, I started experiencing connectivity issues. It seems like my code was just lucky to work before because it didn't follow the recommended practices. For example ...

Vue Vuex computed property fails to refresh

Utilizing vuex and socket.io for updating state.Nodes.nodes, specifically focusing on single node (index 4). Interestingly, despite logging output_state and computed properties to be the same initially, they differ after mutation! computed: { tmpStatus ...

Unable to replicate the functionality of the tab key using jQuery for the enter key

How can I focus on the first input ('Qtd on the table') after pressing enter on the 'Buscar' input? I've tried various solutions like $(this).nextAll('input').first().focus(); $(this).next('input:text').focus ...

Best practices for working with HTML elements using JavaScript

What is the best approach for manipulating HTML controls? One way is to create HTML elements dynamically: var select = document.getElementById("MyList"); var options = ["1", "2", "3", "4", "5"]; for (var i = 0; i < options.length; i++) { var opt = ...

There seems to be an issue with the AJAX REST call failing to transmit data

Whenever I attempt to submit the form, the page refreshes and nothing gets saved in the database. The code in subforum.js $(document).on('click','#submit',function(e) { var user = JSON.parse(sessionStorage.getItem("ulogovan")); consol ...

`What purpose does the data-view attribute serve in Durandal JS?`

While experimenting with the Durandal JS starter kit application, I happened to view the page source in Mozilla browser and noticed the following. <div class="" data-view="views/shell" style="" data-active-view="true"> </div> I couldn't ...