How to focus v-textarea programatically in vuetify and typescript?

10,694

Solution 1

Vuetify does not always work when focusing input, even with $nextTick.

This is how we do it generically for both input and textarea. We actually use this code with the ref set to our form in order to focus the first visible textarea or input. However, you can target just the widget that suits your needs.

mounted() {
  this.$nextTick(() => {
          const theElement = this.$refs.myRef         
          const input = theElement.querySelector('input:not([type=hidden]),textarea:not([type=hidden])')
          if (input) {
            setTimeout(() => {
              input.focus()
            }, 0)
          }
  });
}

The delay for $nextTick and setTimeout is negligible, often required, and will save you time and time again.

You don't need to exclude type=hidden either, but it can't hurt.

If the ref is not an HTMLElement, but instead a VueComponent, you may need to use this.$refs.myRef.$el to get the DOM element.

Solution 2

I got mine to work with this:

mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.$refs.descriptionDescription.$refs.input.focus()
      })
    })
  },

Combination of code from @Thomas and @Steven Spungin

Solution 3

<template>
   <v-text-area
      ref="myTextArea"
   />
</template>

<script>
   ...
   created () {
      this.$refs["myTextArea"].$refs.textarea.focus()
   }
</script>

I did it like this for text-fields. I think it should work the same way.

Solution 4

you can use the autofocus property

<v-text-field
                        autofocus
                        label="Insert Document Number*"
                        single-line
                      ></v-text-field>

Solution 5

work for me

setTimeout(() => {
   this.$refs.textNote.$refs.input.focus()
})
Share:
10,694

Related videos on Youtube

Renetik
Author by

Renetik

Creating various software and libraries with passion starting from 2005. My name is Rene Dohan, professional software developer originally from Slovakia. I have broad scale of abilities and I make and did software, such as native mobile applications for iOS and Android, progressive web apps in typescript and Vue, backend development mostly Java/Kotlin and desktop apps as well (Various platforms). Now I am focused on Android and iOS. I am potentially open for new projects and long term cooperation if I can work independently or as a project lead developer. I always have some projects ongoing, but I am able to handle multiple distinct projects, if I am in charge of whole codebase I work on. I like to work focused on product success, take it as my own and improve and suggest and build to de best possible quality and detail I currently am able to. I love to use new software technologies and frameworks, but always looking to no over-complicate things, make logic simple, readable and easy to understand next time I will have to look at it, what is good for others as well. I like to create long term relations/conEdence with clients/companies I am working with. Dealing with people with respect is priority, but also with focus on delivery of quality product, so to express valuable opinions even if different from others is a must. With my nomad style of living, I prefer remote positions, currently I am in Mexico. Favorite editor: Android Studio, AppCode/Xcode, IntelliJIdea • First computer: Intel 386 SX When I was 15 years old I bought my first Intel 386 SX in Vancouver, Canada and brought it to Bratislava.and it stopped working suddenly. I completely disassembled it and found factory defect in power supply.I was able to Exit actually and I re-assembled the computer while studying all parts, later some years I actually broke it when changing some pc part. When I was 16 I already started doing some software development so to speak, it was computer virus in assembler.The basic idea was to promote some Anarcho-Communistic ideology to computer users, by forcing them to respond to some simple questions about how ideal world should be like. For wrong answers there was prepared surprise of complete wiping of both FAT tables, that cause almost unrepairable data los.Thanks god the reproduction/copy routine was not so good written and I subsequently gave up on idea altogether. I also played a little with pascal but then abandoned software development and focused on computer hardware and playing games. I then fallen in love with computer hardware, tuning of by bios configurations to accelerate PC and hardware cooling etc, until some years later I came back to software again in one of my first jobs.

Updated on September 14, 2022

Comments

  • Renetik
    Renetik over 1 year

    Seems like impossible now, I tried some crazy stuff like:

    (this.refs.vtextarea as any).textarea.focus()
    
    ((this.refs.vtextarea as Vue).$el as HTMLElement).focus()
    

    etc...

    Javascript source is hardly readable for me but it's sad to had to do it like this...

    Int this some basic stuff, or am I missing something obvious ?

    PS: well I see textarea element somewhere there in hierarchy... Maybe I can access by some basic dom child element access way... but this will be like writing worst code of my life.

  • Renetik
    Renetik almost 5 years
    Sadly its not about the next tick here, I call it when it is already rendered and ready.
  • Meena Chaudhary
    Meena Chaudhary about 4 years
    for me this.$refs["myTextArea"].$refs.input.focus(); worked.
  • Marcello B.
    Marcello B. almost 3 years
    This is the only solution that worked for me.
  • Marcello B.
    Marcello B. almost 3 years
    This is great for the first load.